home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / system-a / sag-0.000 / sag-0.3.unoffical.txt
Encoding:
Text File  |  1995-08-08  |  225.1 KB  |  5,122 lines

  1.  
  2. Linux System Administrator's Guide 0.3
  3.  
  4.  
  5. Linux System Administrator's Guide 0.3
  6.  
  7.  
  8. Lars Wirzenius
  9.                       The Linux Documentation Project
  10.  
  11. This is version 0.3 of the Linux System Administrators' Guide.
  12.  
  13. Published August 6, 1995.
  14.  
  15. The LATEX source code and other machine readable formats can be found on the
  16. Internet via anonymous ftp on sunsite.unc.edu, in the directory
  17. /pub/Linux/docs/LDP.
  18.  
  19. Also available are Postscript and TEX .DVI formats, and possibly a plain text
  20. version (to be released after the other formats). HTML versions may also be
  21. forthcoming.
  22.  
  23. Copyright cO1993, 1995 Lars Wirzenius.
  24.  
  25. Hernesaarenkatu 15 A 2, Fin-00150 Helsinki, Finland, lars.wirzenius@helsinki.fi.
  26.  
  27. UNIX is a trademark of Novell, Inc. Linux is not a trademark, and has no
  28. connection to UNIX TM or Novell.
  29.  
  30. Permission is granted to make and distribute verbatim copies of this manual
  31. provided the copyright notice and this permission notice are preserved on all
  32. copies.
  33.  
  34. Permission is granted to process the document source code through TEX or other
  35. formatters and print the results, provided the printed document carries copying
  36. permission notice identical to this one.
  37.  
  38. Permission is granted to copy and distribute modified versions of this manual
  39. under the conditions for verbatim copying, provided that the entire resulting
  40. derived work is distributed under the terms of a permission
  41. notice identical to this one.
  42.  
  43. Permission is granted to copy and distribute translations of this manual into
  44. another language, under the above conditions for modified versions, except that
  45. this permission notice may be stated in a translation approved by the Free
  46. Software Foundation.
  47.  
  48. The Free Software Foundation may be contacted at:
  49.  
  50.  
  51.     59 Temple Place Suite 330
  52.     Boston, MA 02111-1307 USA
  53.  
  54.  
  55. The appendices not written by Lars Wirzenius are copyrighted by their authors,
  56. and can be copied and distributed only in unmodified form.
  57.  
  58. The author would appreciate a notification of modifications, translations, and
  59. printed versions. Thank you.
  60.  
  61.  
  62.  
  63. This page is dedicated to a future dedication.
  64.  
  65.  
  66.  
  67. Contents
  68.  
  69. 1  Introduction                                                      5
  70.  
  71.    1.1 The Linux Documentation Project   : : : :  : : : : : : : : : : :   8
  72.  
  73.  
  74. 2  Overview of a Linux System                                       9
  75.  
  76.  
  77.    2.1 Various parts of an operating system  : : : : : : : : : : : : : :   9
  78.    2.2 Important parts of the kernel   : : : : : : : : : : : : : : : : :  10
  79.    2.3 Major services in a UNIX system : : : : : : : : : : : : : : : : :  11
  80.    2.4 The filesystem layout : : : : : : : : : : : : : : : : : : : : : :  15
  81.  
  82. 3  Boots And Shutdowns                                            17
  83.  
  84.    3.1 An overview of boots and shutdowns  : : : : : : : : : : : : : :  17
  85.    3.2 The boot process in closer look   : : : : : : : : : : : : : : :  18
  86.    3.3 More about shutdowns  : : : : : : : : : : : : : : : : : : : : :  21
  87.    3.4 Rebooting  : : : : : : : : :: : : : : : : : : : : : : : : : : :  22
  88.    3.5 Single user mode  : : : : : : : : : : : : : : : : : : : : : : :  23
  89.    3.6 Emergency boot floppies : : : : : : : : : : : : : : : : : : : :  23
  90.  
  91. 4  Using Disks and Other Storage Media                             25
  92.  
  93.    4.1 Two kinds of devices  : : : : : : : : :  : : : : : : : : : : : :  26
  94.    4.2 Hard disks  : : : : : : : :  : : : : : : : : : : : : : : : : : :  27
  95.    4.3 Floppies : : : : : : : : : : : : : : : : : : : : : : : : : : : :  30
  96.    4.4 Formatting   : : : : : : : : : : : :: : : : : : : : : : : : : :  31
  97.  
  98.  
  99.                                    i
  100. ii                                                            CONTENTS
  101.  
  102.  
  103.  
  104.    4.5 Partitions  : : : : : : : : : : : : : : : : : : : : : : : : : : : :  33
  105.    4.6 Filesystems : : : : : : : : : : : : : : : : : : : : : : : : : : : :  37
  106.    4.7 Disks without filesystems : : : : : : : : : : : : : : : : : : : : :  47
  107.    4.8 Allocating disk space : : : : : : : : : : : : : : : : : : : : : : :  48
  108.  
  109. 5  Directory Tree Overview                                          53
  110.  
  111.    5.1 Background  : : : : : : : : : : : : : : : : : : : : : : : : : : : :  53
  112.    5.2 The root filesystem : : : : : : : : : : : : : : : : : : : : : : : :  55
  113.    5.3 The /usr filesystem : : : : : : : : : : : : : : : : : : : : : : : :  59
  114.    5.4 The /var filesystem : : : : : : : : : : : : : : : : : : : : : : : :  60
  115.    5.5 The /proc filesystem  : : : : : : : : : : : : : : : : : : : : : : :  61
  116.  
  117. 6  Memory Management                                            63
  118.  
  119.    6.1 What is virtual memory? : : : : : : : : : : : : : : : : : : : : : :  63
  120.    6.2 Creating a swap area  : : : : : : : : : : : : : : : : : : : : : : :  64
  121.    6.3 Using a swap area : : : : : : : : : : : : : : : : : : : : : : : : :  65
  122.    6.4 Sharing swap areas with other operating systems   : : : : : : : : :  66
  123.    6.5 Allocating swap space   : : : : : : : : : : : : : : : : : : : : : :  67
  124.    6.6 The buffer cache    : : : : : : : : : : : : : : : : : : : : : : : :  68
  125.  
  126. 7  Logging In And Out                                              71
  127.  
  128.    7.1 Logins via terminals    : : : : : : : : : : : : : : : : : : : : : :  71
  129.    7.2 Logins via the network  : : : : : : : : : : : : : : : : : : : : : :  72
  130.    7.3 What login does   : : : : : : : : : : : : : : : : : : : : : : : : :  73
  131.    7.4 X and xdm   : : : : : : : : : : : : : : : : : : : : : : : : : : : :  74
  132.    7.5 Access control  : : : : : : : : : : : : : : : : : : : : : : : : : :  74
  133.    7.6 Shell startup : : : : : : : : : : : : : : : : : : : : : : : : : : :  75
  134.  
  135. A  Design and Implementation of the Second Extended Filesystem     77
  136.  
  137.    A.1 History of Linux filesystems  : : : : : : : : : : : : : : : : : : :  78
  138. CONTENTS                                                            iii
  139.  
  140.  
  141.  
  142.    A.2 Basic File System Concepts  : : : : : : : : : : : : : : : : : : : :  79
  143.    A.3 The Virtual File System   : : : : : : : : : : : : : : : : : : : : :  82
  144.    A.4 The Second Extended File System : : : : : : : : : : : : : : : : : :  83
  145.    A.5 The Ext2fs library    : : : : : : : : : : : : : : : : : : : : : : :  88
  146.    A.6 The Ext2fs tools    : : : : : : : : : : : : : : : : : : : : : : : :  89
  147.    A.7 Performance Measurements    : : : : : : : : : : : : : : : : : : : :  91
  148.    A.8 Conclusion  : : : : : : : : : : : : : : : : : : : : : : : : : : : :  93
  149.  
  150. B  Measuring Holes                                                 97
  151.  
  152. C  The Linux Device List                                            99
  153.  
  154.    C.1 Introduction  : : : : : : : : : : : : : : : : : : : : : : : : : : :  99
  155.    C.2 Major numbers : : : : : : : : : : : : : : : : : : : : : : : : : : : 100
  156.    C.3 Minor numbers : : : : : : : : : : : : : : : : : : : : : : : : : : : 101
  157.    C.4 Additional /dev directory entries   : : : : : : : : : : : : : : : : 115
  158. iv                                                           CONTENTS
  159.  
  160.  
  161.  
  162.  
  163.  
  164. Introduction  to  the  ALPHA
  165.  
  166.  
  167.  
  168. Versions
  169.  
  170.  
  171.  
  172. In the beginning, the file was without form, and void; and
  173. emptiness was upon the face of the bits. And the Fingers of
  174. the Author moved upon the face of the keyboard. And the Author said, Let there
  175. be words, and there were words.
  176.  
  177.  
  178.    This is an ALPHA version of the Linux System Administrators' Guide. That
  179. means that I don't even pretend it contains anything useful, or that anything
  180. contained within it is factually correct. In fact, if you believe anything that
  181. I say in this version, and you are hurt because of it, I will cruelly laugh at
  182. your face if you complain.
  183.  
  184.    Well, almost.  I won't laugh, but I also will not consider myself responsible
  185. for anything.
  186.  
  187.    The purpose of an ALPHA version is to get the stuff out so that other people
  188. can look at it and comment on it. The latter part is the important one: Unless
  189. the author gets feedback, the ALPHA version isn't doing anything good.
  190. Therefore, if you read this `book', please, please, please let me hear your
  191. opinion about it.  I don't care whether you think it is good or bad, I want you
  192. to tell me about it.
  193.  
  194.    If at all possible, you should mail your comments directly to me, otherwise
  195. there is a largish chance I will miss them. If you want to discuss things in
  196. public (on one of the comp.os.linux newsgroups or the mailing list), that is ok
  197. by me, but please send a copy via mail directly to me as well.
  198.  
  199.  
  200.    I do not much care about the format in which you send your comments, but it
  201. is essential that you clearly indicate what part of my text you are commenting
  202. on. I can be contacted at the following e-mail addresses:
  203.  
  204.  
  205.    lars.wirzenius@helsinki.fi
  206.  
  207.  
  208.                                    1
  209. 2                                                            CONTENTS
  210.  
  211.  
  212.  
  213.    wirzeniu@cc.helsinki.fi
  214.  
  215.    wirzeniu@cs.helsinki.fi
  216.  
  217.    wirzeniu@kruuna.helsinki.fi
  218.  
  219.    wirzeniu@hydra.helsinki.fi
  220.  
  221. (they're all actually the same account, but I give all these, just in case there
  222. is some weird problem).
  223. This text contains a lot of notes that I have inserted as notes to myself. They
  224. are identified with \META: ". They indicate things that need to be worked on,
  225. that are missing, that are wrong, or something like that. They are mostly for my
  226. own benefit and for your amusement, they are not things that I am hoping someone
  227. else will write for me.
  228.  
  229.  
  230.    If you think that this version of the manual is missing a lot, you are right.
  231. I am including only those chapters that are at least half finished.  New
  232. chapters will be released as they are written.
  233. CONTENTS                                                             3
  234.  
  235.  
  236.  
  237.    The LDP Rhyme1
  238.  
  239.  
  240.    A wondrous thing,                   We started to write,
  241.    and beautiful,                      or plan, at least,
  242.    'tis to write,                      several books,
  243.    a book.                             one for every need.
  244.  
  245.  
  246.    I'd like to sing,                   The start was fun,
  247.    of the sweat,                       a lot of talk,
  248.    the blood and tear,                 an outline,
  249.    which it also took.                 then a slew.
  250.  
  251.  
  252.    It started back in,                 Then silence came,
  253.    nineteen-ninety-two,                the work began,
  254.    when users whined,                  some wrote less,
  255.    "we can nothing do!"                others more.
  256.  
  257.  
  258.    They wanted to know,                A blank screen,
  259.    what their problem was,             oh its horrible,
  260.    and how to fix it                   it sits there,
  261.    (by yesterday).                     laughs in the face.
  262.  
  263.  
  264.    We put the answers in,              We still await,
  265.    a Linux f-a-q,                      the final day,
  266.    hoped to get away,                  when everything,
  267.    from any more writin'.              will be done.
  268.  
  269.  
  270.    "That's too long,                   Until then,
  271.    it's hard to search,                all we have,
  272.    and we don't read it,               is a draft,
  273.    any-which-way!"                     for you to comment on.
  274.  
  275.  
  276.    Then a few of us,
  277.    joined toghether
  278.    (virtually, you know),
  279.    to start the LDP.
  280.  
  281.  
  282. ______________1
  283.    The author wishes to remain anonymous.  It was
  284. posted to the LDP mailing list by Matt Welsh.
  285. 4                                                            CONTENTS
  286.  
  287.  
  288.  
  289.  
  290.  
  291. Chapter  1
  292.  
  293.  
  294.  
  295. Introduction
  296.                                   I pride myself on the fact that my work has
  297.  
  298.                                                no socially redeeming value.
  299.  
  300.                                                           (John Waters)
  301.    This manual, the Linux System Administrators' Guide, describes the system
  302. admin-istration aspects of using Linux. It is intended for people who know next
  303. to nothing about system administration (as in \what is it?"), but who have
  304. already mastered at least the basics of normal usage, which means roughly the
  305. material covered by the Linux Users' Guide. This manual also doesn't tell you
  306. how to install Linux; that is described in the Installation and Getting Started
  307. document. There is some overlap between all the Linux Documentation Project
  308. manuals, but they all look at things from slightly different angles. See below
  309. for more information about Linux manuals.
  310.  
  311.    What, then, is system administration? It is all the things that one has to do
  312. to keep a computer system in a useable shape. Things like backing up files (and
  313. restoring them if necessary), installing new programs, creating accounts for
  314. users (and deleting them when no longer needed), making certain that the
  315. filesystem is not corrupted, and so on. If a computer were, say, a house, system
  316. administration would be called maintenance, and would include cleaning, fixing
  317. broken windows, and other such things. System administration is not called
  318. maintenance, because that would be too simple. 1
  319.  
  320.  
  321.    The structure of this manual is such that many of the chapters should be
  322. usable independently, so that if you need information about, say, backups, you
  323. can read just 
  324.  
  325. _____________________________1
  326.    There are some people who do call it that, but that's just because they have
  327. never read this manual, poor things.
  328.  
  329.  
  330.                                    5
  331. 6                                                Chapter 1.  Introduction
  332.  
  333.  
  334.  
  335. that chapter.2 This hopefully makes the book easier to use as a reference
  336. manual, and makes it possible to read just a small part when needed, instead of
  337. having to read everything.  However, this manual is first and foremost a
  338. tutorial, and a reference manual only as a lucky coincidence.
  339.  
  340.  
  341.    This manual is not intended to be used completely by itself. Plenty of the
  342. rest of the Linux documentation is also important for system administrators.
  343. After all, a system administrator is just a user with special privileges and
  344. duties.  A very important resource is the man pages, which should always be
  345. consulted when a command is not familiar.
  346.  
  347.    While this manual is targeted at Linux, a general principle has been that it
  348. should be useful with other UNIX based operating systems as well. 
  349. Unfortunately, since there is so much variance between different versions of
  350. UNIX in general, and in system administration in particular, there is little
  351. hope to cover all variants. Even covering all possibilities for Linux is
  352. difficult, due to the nature of its development.  There is no one official Linux
  353. distribution, so different people have different setups, many people have a
  354. setup they have built up themselves. When possible, I have tried to point out
  355. differences, and explain several alternatives. In order to cater to the hackers
  356. and DIY types that form the driving force behind Linux development, I have tried
  357. to describe how things work, rather than just listing \five easy steps" for each
  358. task. This means that there is much information here that is not necessary for
  359. everyone, but those parts are marked as such and can be skipped if you use a
  360. preconfigured system. Reading everything will, naturally, increase your
  361. understanding of the system and should make using and administering it more
  362. pleasant.
  363.  
  364.    Like all other Linux related development, the work was done on a volunteer
  365. basis: I did it because I thought it might be fun and because I felt it should
  366. be done. However, like all volunteer work, there is a limit to how much effort I
  367. have been able to spend, and also on how much knowledge and experience I have.
  368. This means that the manual is not necessarily as good as it would be if a wizard
  369. had been paid handsomely to write it and had spent a few years to perfect it. I
  370. think, of course, that it is pretty nice, but be warned.
  371.  
  372.  
  373.    One particular point where I have cut corners is that I have not covered very
  374. thoroughly many things that are already well documented in other freely
  375. available manuals. This applies especially to program specific documentation,
  376. such as all the details of using mkfs(8). I only describe the purpose of the
  377. program, and as much
  378.  
  379. _____________________________2
  380.    If you happen to be reading a version that has a chapter on backups, that is.
  381.                                                                       7
  382.  
  383.  
  384.  
  385. of its usage as is necessary for the purposes of this manual.  For further
  386. informa-tion, I refer the gentle reader to these other manuals. Usually, all of
  387. the referred to documentation is part of the full Linux documentation set.
  388.  
  389.  
  390.    While I have tried to make this manual as good as possible, I would really
  391. like to hear from you if you have any ideas on how to make it better.  Bad
  392. language, factual errors, ideas for new areas to cover, rewritten sections,
  393. information about how various UNIX versions do things, I am interested in all of
  394. it. You can contact me via electronic mail with the Internet domain address
  395. lars.wirzenius@helsinki.fi, or
  396.  
  397. by traditional paper mail using the address
  398.  
  399.  
  400.     Lars Wirzenius / Linux docs
  401.     Hernesaarentie 15 A 2
  402.     00150 Helsinki
  403.     Finland
  404.  
  405.  
  406.    Many people have helped me with this book, directly or indirectly.  I would
  407. like to especially thank Matt Welsh for inspiration and LDP leadership, Andy
  408. Oram for igniting an almost dead spark again with much-valued feedback, Olaf
  409. Kirch for showing me that it can be done, and Adam Richter at Yggdrasil and
  410. others for showing me that other people can find it interesting as well.
  411.  
  412.    H. Peter Anvin, R'emy Card, Theodore Ts'o, and Stephen Tweedie have let me
  413. borrow their work (and thus make the book look thicker and much more
  414. impressive). I am most grateful for this, and very apologetic for the earlier
  415. versions that sometimes lacked proper attribution. Stephen Tweedie also let me
  416. borrow his comparison of the xia and ext2 filesystems, but that has since been
  417. dropped, since xia is no longer very popular.
  418.  
  419.  
  420.    In addition, I would like to thank Mark Komarinski for sending his material
  421. in 1993 and the many system administration columns in Linux Journal. They are
  422. quite informative.
  423.  
  424.  
  425.    Thanks to Erik Troan at Red Hat, for promising to make a plain text version
  426. of this book available.3
  427.  
  428.  
  429.    A minor accusation goes to Linus Torvalds for writing the damn system to
  430. write about in the first place.  That applies for the rest of
  431. /usr/src/linux/CREDITS as well. Be ashamed, be very ashamed.
  432.  
  433.  
  434.    Many useful comments have been sent by a large number of people. My
  435. miniatureblack hole of an archive doesn't let me find all their names, but some
  436. of them are in
  437. _____________________________3
  438.    Erik, you can color yourself pressurized.
  439. 8                                                Chapter 1.  Introduction
  440.  
  441.  
  442.  
  443. alphabetical order: Paul Caprioli, Ales Cepek, Marie-France Declerfayt, Olaf
  444. Flebbe, Helmut Geyer, Larry Greenfield and his father, Stephen Harris, Jyrki
  445. Havia, Jim Haynes, York Lam, Timothy Andrew Lister, Jim Lynch, Dan Poirier,
  446. Daniel Quinlan, Philippe Steindl. My apologies to anyone I have forgotten.
  447.  
  448. 1.1   The Linux Documentation Project
  449.  
  450.  
  451.  
  452. The Linux Documentation Project, or LDP, is a loose team of writers,
  453. proofreaders, and editors who are working together to provide complete
  454. documentation for the Linux operating system. The overall coordinator of the
  455. project is Matt Welsh, who is aided by Lars Wirzenius and Michael K. Johnson.
  456.  
  457.  
  458.    This manual is one in a set of several being distributed by the LDP,
  459. including a Linux Users' Guide, System Administrators' Guide, Network
  460. Administrators' Guide, and Kernel Hackers' Guide. These manuals are all
  461. available in LATEX source format, .dvi format, and postscript output by
  462. anonymous FTP from sunsite.unc.edu, in the directory /pub/Linux/docs/LDP, and
  463. from tsx-11.mit.edu, in the directory /pub/linux/docs/guides.
  464.  
  465.  
  466.    We encourage anyone with a penchant for writing or editing to join us in
  467. improving Linux documentation. If you have Internet e-mail access, you can
  468. contact Matt Welsh at mdw@sunsite.unc.edu.
  469.  
  470.  
  471. Chapter  2
  472.  
  473.  
  474.  
  475. Overview  of  a  Linux  System
  476.  
  477.  
  478.  
  479.                                                        A quote is needed.
  480. This chapter gives an overivew of a Linux system. First, the major services
  481. provided by the operating system are described.  Then, the programs that
  482. implement these services are described with a considerable lack of detail. The
  483. purpose of this chapter is to give an understanding of the system as a whole, so
  484. each part is described in detail elsewhere.
  485.  
  486. 2.1   Various parts of an operating system
  487.  
  488.  
  489.  
  490. A UNIX operating system consists of a kernel and some system programs. There 
  491. also some application programs for doing work.  The kernel is the heart of the 
  492. operating system1. It keeps track of files on the disk, starts programs and
  493. multiplexes the processor and other hardware between them to provide
  494. multitasking, assigns memory and other resources to various processes, receives
  495. packets from and sends packets to the network, and so on. The kernel does very
  496. little by itself, but it provides tools with which all services can be built. It
  497. also prevents anyone from accessing the hardware directly, forcing everyone to
  498. use the tools it provides. This way the kernel can control who gets to do what
  499. and can provide some protection for users from each other. The tools provided by
  500. the kernel are used via system calls; see manual page section 2 for more
  501. information on these.
  502.  
  503.  
  504.    The system programs use the tools provided by the kernel to implement the
  505. var-
  506. _____________________________1
  507.    In fact, it is often mistakenly considered to be the operating system itself,
  508. but it is not. An operating system
  509. provides many more services than a plain kernel.
  510.  
  511.  
  512.                                    9
  513. 10                                   Chapter 2.  Overview of a Linux System
  514.  
  515.  
  516.  
  517. ious services required from an operating system.  System programs, and all other
  518. programs, run `on top of the kernel', in what is called the user mode. The
  519. difference between system and application programs is one of intent:
  520. applications are intended for getting useful things done (or for playing, if it
  521. happens to be a game), whereas system programs are needed to get the system
  522. working.  A word processor is an application; telnet is a system program. The
  523. difference is often somewhat blurry, however, and is important only to
  524. compulsive categorizers.
  525.  
  526.  
  527.    An operating system can also contain compilers and their corresponding
  528. libraries (GCC and the C library in particular under Linux), although not all
  529. programming languages need be part of the operating system. Documentation, and
  530. sometimes even games, can also be part of it. Traditionally, the oeprating
  531. system has been defined by the contents of the installation tape or disks; with
  532. Linux it is not as clear since the stupid thing is spread all over the FTP sites
  533. of the world.
  534.  
  535. 2.2   Important parts of the kernel
  536.  
  537.  
  538.  
  539. The Linux kernel consists of several important parts: process management, memory
  540. management, hardware device drivers, filesystem drivers, network management, and
  541. various other bits and pieces. Figure 2.1 shows some of them.
  542.  
  543.    Probably the most important parts of the kernel (nothing else works without
  544. them) are the memory management and the process management.  Memory management
  545. takes care of assigning memory areas and swap space areas to processes, parts of
  546. the kernel, and for the buffer cache.  Process management creates processes, and
  547. implements the multitasking by switching the active process on the processor.
  548.  
  549.    At the lowest level, the kernel contains a hardware device driver for each
  550. kind of hardware it supports.  Since the world is full of different kinds of
  551. hardware, the number of hardware device drivers is large. There are often many
  552. otherwise similar pieces of hardware that differ in how they are controlled by
  553. software. The similarities make it possible to have general classes of drivers
  554. that support similar operations; each member of the class has the same interface
  555. to the rest of the kernel but differs in what it needs to do to implement them.
  556. For example, all hard disk drivers look alike to the rest of the kernel, i.e.,
  557. they all have operations like `initialize the drive', `read sector N', and
  558. `write sector N'.
  559.  
  560.    Some software services provided by the kernel itself have similar properties.
  561. For example, the various network protocols have been abstracted into one
  562. programming interface, the BSD socket library. Another example are the various
  563. filesystems Linux 
  564.  
  565. 2.3.  Major services in a UNIX system                                         11
  566.  
  567.  
  568.  
  569.             Figure 2.1: Some of the more important parts of the Linux kernel.
  570. supports: the kernel contains a virtual filesystem (VFS) that contains all the
  571. op-erations for a filesystem, and a filesystem driver for each supported
  572. filesystem. When some entity tries to use a filesystem, the request goes via the
  573. VFS, which routes the request to the proper filesystem driver.
  574.  
  575. 2.3   Major services in a UNIX system
  576.  
  577. This section describes some of the more important UNIX services, but without
  578. much detail. They are described more thorougly in later chapters.
  579. 12                                   Chapter 2.  Overview of a Linux System
  580.  
  581.  
  582.  
  583. 2.3.1  init
  584.  
  585.  
  586.  
  587. The single most important service in a UNIX system is provided by init. init is
  588. started as the first process of every UNIX system, as the last thing the kernel
  589. does when it boots.  When init starts, it continues the boot process by doing
  590. various startup chores (checking and mounting filesystems, starting daemons,
  591. etc).
  592.  
  593.    The exact list of things that init does depends on which flavor it is; there
  594. are several to choose from. init usually provides the concept of single user
  595. mode, in which no one can log in and root uses a shell at the console; the usual
  596. mode is called multiuser mode. Some flavors generalize this as run levels;
  597. single and multiuser modes are considered to be two run levels, and there can be
  598. additional ones as well, for example, to run X on the console.
  599.  
  600.  
  601.    When the system is running, the two most important tasks of init is to make
  602. sure gettys are working (to make sure logins work), that various daemons are
  603. running, and to adopt orphan processes (processes whose parent has died; in UNIX
  604. all processes must be in a single tree, so orphans must be adopted).
  605.  
  606.  
  607.    When the system is shut down, it is init that is in charge of killing all
  608. other processes, unmounting all filesystems and stopping the processor, along
  609. with anything else that it feels like doing.
  610.  
  611. 2.3.2  Logins from terminals
  612.  
  613.  
  614.  
  615. Logins from terminals (via serial lines) and the console (when not running X)
  616. are provided by the getty program. init starts a separate instance of getty for
  617. each terminal for which logins are to be allowed. getty reads the username and
  618. runs the login program, which reads the password.  If the username and password
  619. match, login runs the shell.  When the shell terminates, i.e., the user logs
  620. out, or when login terminated because the username and password didn't match,
  621. init notices this and starts a new instance of getty. The kernel has no notion
  622. of logins, this is all handled by the system programs.
  623.  
  624. 2.3.3  Syslog
  625.  
  626.  
  627.  
  628. The kernel and many system programs produce error, warning, and other messages.
  629. It is often important that these messages can be viewed later, even much later,
  630. so they should be written to a file. The program doing this is syslog. It can be
  631. configured to sort the messages to different files according to writer or degree
  632. of importance.
  633. 2.3.  Major services in a UNIX system                                         13
  634.  
  635.  
  636.  
  637. For example, kernel messages are often directed to a separate file from the
  638. others, since kernel messages are often more important and need to be read
  639. regularly to spot problems.
  640.  
  641. 2.3.4  Periodic command execution: cron and at
  642.  
  643.  
  644.  
  645. Both users and the system administrator often need to run specific commands
  646. peri-odically. For example, the system administrator might want to run a command
  647. to clean the directories with temporary files (/tmp and /var/tmp) from old
  648. files, to keep the disks from filling up, since not all programs clean up after
  649. themselves correctly. 
  650.  
  651.    The cron service is set up to do this. Each user has a crontab, where he
  652. lists the commands he wants to execute and the times they should be executed.
  653. The crond daemon takes care of starting the commands when specified.
  654.  
  655.  
  656.    The at service is similar to cron, but it is once only: the command is
  657. executed at the given time, but it is not repeated.
  658.  
  659. 2.3.5  Graphical user interface
  660.  
  661.  
  662.  
  663. UNIX and Linux don't incorporate the user interface into the kernel; instead,
  664. they let it be implemented by user level programs. This applies for both text
  665. mode and graphical environments.
  666.  
  667.  
  668.    This arrangement makes the system more flexible, but has the disadvantage
  669. that it is simple to implement a different user interface for each program,
  670. making the system harder to learn.
  671.  
  672.  
  673.    The graphical environment primarily used with Linux is called the X Window
  674. System (X for short). X also does not implement a user interface; it only
  675. implements a window system, i.e., tools with which a graphical user interface
  676. can be implemented.
  677.  
  678. The three most popular user interface styles implemented over X are Athena,
  679. Motif, and Open Look.
  680.  
  681. 2.3.6  Networking
  682.  
  683.  
  684.  
  685. Networking is the act of connecting two or more computers so that the can commu-
  686. nicate with each other.  The actual methods of connecting and communicating are
  687. slightly complicated, but the end result is very attractive.
  688. 14                                   Chapter 2.  Overview of a Linux System
  689.  
  690.  
  691.  
  692.    UNIX operating systems have many networking features.  Most basic services_
  693. filesystems, printing, backups, etc_can be done over the network.  This can make
  694. system administration easier, since it allows centralized administration, while
  695. still reaping in the benefits of microcomputing and distributed computing, such
  696. as lower costs and better fault tolerance.
  697.  
  698.  
  699.    However, this book merely glances at networking; see the Linux Network
  700. Admin-istrators' Guide for more information, including a basic descriptions of
  701. how networks operate.
  702.  
  703. 2.3.7  Network logins
  704.  
  705.  
  706.  
  707. Network logins work a little differently than normal logins. There is a separate
  708. phys-ical serial line for each terminal via which it is possible to log in. For
  709. each person logging in via the network, there is a separate virtual network
  710. connection, and there can be any number of these2. It is therefore not possible
  711. to run a separate getty for each possible virtual connection. There are also
  712. several different ways to log in via network, telnet and rlogin being the major
  713. ones in TCP/IP networks.
  714.  
  715.  
  716.    Network logins have, instead of a herd of gettys, a single daemon (per way of
  717. logging in; telnet and rlogin have separate daemons) that listens for all
  718. incoming login attempts. When it notices one, it starts a new instance of itself
  719. to handle that single attempt; the original instance continues to listen for
  720. other attempts. The new instance works similarly to getty.
  721.  
  722. 2.3.8  Network file systems
  723.  
  724.  
  725.  
  726. One of the more useful things that can be done with networking services is
  727. sharing files via a network file system.  The one usually used is called the
  728. Network File System, or NFS, developed by Sun.
  729.  
  730.  
  731.    With a network file system any file operations done by a program on one
  732. machine are sent over the network to another computer.  This fools the program
  733. to think that all the files on the other computer are actually on the computer
  734. the program is running on. This makes information sharing extremely simple,
  735. since it requires no modifications to programs.
  736.  
  737. _____________________________2
  738.    Well, at least there can be many. Network bandwidth still being a scarce
  739. resource, there is still some practical upper limit to the number of concurrent
  740. logins via one network connection.
  741. 2.4.  The filesystem layout                                                  15
  742.  
  743.  
  744.  
  745. 2.3.9  Mail
  746.  
  747.  
  748.  
  749. Electronic mail is usually the most important method for communicating via com-
  750. puter. An electronic letter is stored in a file using a special format, and
  751. special mail programs are used to send and read the letters.
  752.  
  753.  
  754.    Each user has an incoming mailbox (a file in the special format), where all
  755. new mail is stored. When someone sends a mail, the mail program locates the
  756. receiver's mailbox and appends the letter to the mailbox file.  If the
  757. receiver's mailbox is in an another machine, the letter is sent to the other
  758. machine, which delivers it to the mailbox as it best sees fit.
  759.  
  760.  
  761.    The mail system consists of many programs. The delivery of mail to local or
  762. remote mailboxes is done by one program (e.g., sendmail or smail), while the
  763. programs users use are many and varied (e.g., Pine or elm). The mailboxes are
  764. usually stored in /var/spool/mail.
  765.  
  766. 2.3.10  Printing
  767.  
  768.  
  769.  
  770. Only one person can use a printer at one time, but it is uneconomical not to
  771. share printers between users. The printer is therefore managed by software that
  772. implements a print queue: all print jobs are put into a queue and whenever the
  773. printer is done with one job, the next one is sent to it automatically. This
  774. relieves the users from organizing the print queue and fighting over control of
  775. the printer.3
  776.  
  777.  
  778.    The print queue software also spools the printouts on disk, i.e., the text is
  779. kept in a file while the job is in the queue. This allows an application program
  780. to spit out the print jobs quickly to the print queue software; the application
  781. does not have to wait until the job is actually printed to continue. This is
  782. really convenient, since itallows one to print out one version, and not have to
  783. wait for it to be printed before one can make a completely revised new version.
  784.  
  785. 2.4   The filesystem layout
  786.  
  787.  
  788.  
  789. The filesystem is divided into many parts; usually along the lines of a root
  790. filesystem with /bin, /lib, /etc, /dev, and a few others; a /usr filesystem with
  791. programs and unchanging data; a /var filesystem with changing data (such as log
  792. files); and a /home
  793. _____________________________3
  794.    Instead, they form a new queue at the printer, waiting for their printouts,
  795. since no-one ever seems to be able to get the queue software to know exactly
  796. when anyone's printout is really finished. This is a great boot for intra-office
  797. social relations.
  798. 16                                   Chapter 2.  Overview of a Linux System
  799.  
  800.  
  801.  
  802. filesystem for everyone's personal files. Depending on the hardware
  803. configuration and the decisions of the system administrator, the division can be
  804. different; it can even be all in one filesystem.
  805.  
  806.  
  807.    Chapter 5 describes the filesystem layout in some detail; the Linux
  808. Filesystem Standard covers it in somewhat more detail.
  809.  
  810.  
  811. Chapter  3
  812.  
  813.  
  814.  
  815. Boots  And  Shutdowns
  816.  
  817.  
  818.  
  819.                              This chapter needs a quote. Suggestions, anyone?
  820. This section explains what goes on when a Linux system is turned on and off, and
  821. how it should be done properly.
  822.  
  823. 3.1   An overview of boots and shutdowns
  824.  
  825.    The act of turning on a computer system and making its operating system to be
  826. loaded1 is called booting. The name comes from an image of the computer pulling
  827. itself up from its bootstraps, but the act itself slightly more realistic.
  828.  
  829.    During bootstrapping the computer first loads a small piece of code called
  830. the bootstrap loader, which in turn loads and starts the operating system. The
  831. boot-strap loader is usually stored in a fixed location on a hard disk or a
  832. floppy. The reason for this two step process is that the operating system is big
  833. and complicated, but the first piece of code that the computer loads must be
  834. very small (a few hundred bytes), to avoid making the hardware unnecessarily
  835. complicated.
  836.  
  837.  
  838.    Different computers do the bootstrapping differently.  For PC's, the computer
  839. (well, it's BIOS) reads in the first sector (called the boot sector) of a floppy
  840. or hard disk. The bootstrap loader is contained withing this sector. It loads
  841. the operating system from elsewhere on the disk (or from some other place). 
  842.  
  843.    After Linux has been loaded, it initializes the hardware and device drivers,
  844. and
  845. _____________________________1
  846.    On early computers, it wasn't enough to merely turn on the computer, you had
  847. to manually load the operating system as well. These new-fangled thing-a-ma-gigs
  848. do it all by themselves.
  849.  
  850.  
  851.                                   17
  852. 18                                        Chapter 3.  Boots And Shutdowns
  853.  
  854.  
  855.  
  856. then runs init(8). init starts other processes to allow users to log in, and do
  857. things. The details of this part will be discussed below.
  858.  
  859.  
  860.    In order to shut down a Linux system, first all processes are told to
  861. terminate (this makes them close any files and do other necessary things to keep
  862. things tidy), then filesystems and swap areas are unmounted, and finally a
  863. message is printed to the console that the power can be turned off.  If the
  864. proper procedure is not followed, terrible things can and will happen; most
  865. importantly, the filesystem buffer cache might not be flushed, which means that
  866. all data in it is lost and the filesystem on disk is inconsistent, and therefore
  867. possibly unusable.
  868.  
  869. 3.2   The boot process in closer look
  870.  
  871. You can boot Linux either from a floppy or from the hard disk.  The installation
  872. section in the Getting Started guide tells you how to install Linux so you can
  873. boot it the way you want to.
  874.  
  875.  
  876.    When the computer is booted, the BIOS will do various tests to check that
  877. ev-erything looks all-right,2 and will then start the actual booting. It will
  878. choose a disk drive (typically the first floppy drive, if there is a floppy
  879. inserted, otherwise the first hard disk, if one is installed in the computer;
  880. the order might be configurable, how-ever) and will then read its very first
  881. sector. This is called the boot sector; for a hard disk, it is also called the
  882. master boot record, since a hard disk can contain several partitions, each with
  883. their own boot sectors.
  884.  
  885.    The boot sector contains a small program (small enough to fit into one
  886. sector) whose responsibility is to read the actual operating system from the
  887. disk and start it. When booting Linux from a floppy disk, the boot sector
  888. contains code that just reads the first few hundred blocks (depending on the
  889. actual kernel size, of course) to a predetermined place in memory. On a Linux
  890. boot floppy, there is no filesystem, the kernel is just stored in consecutive
  891. sectors, since this simplifies the boot process. It is possible, however, to
  892. boot from a floppy with a filesystem, by using LILO.
  893.  
  894.    When booting from the hard disk, the code in the master boot record will
  895. examine the partition table (also in the master boot record), identify the
  896. active partition (the partition that is marked to be bootable), read the boot
  897. sector from that partition, and then start the code in that boot sector. The
  898. code in the partition's boot sector does what a floppy disk's boot sector does:
  899. it will read in the kernel from the partition and start it.  The details vary,
  900. however, since it is generally not useful to have a
  901. _____________________________2
  902.    These is called the power on self test, or POST for short.
  903. 3.2.  The boot process in closer look                                          
  904. 19
  905.  
  906.  
  907.  
  908. separate partition for just the kernel image, so the code in the partition's
  909. boot sector can't just read the disk in sequential order, it has to find the
  910. sectors whereever thefilesystem has put them. There are several ways around this
  911. problem, but the most common way is to use LILO. (The details about how to do
  912. this are irrelevant for this discussion, however; see the LILO documentation for
  913. more information, it is most thorough.)
  914.  
  915.  
  916.    When booting with LILO, it will normally go right ahead and read in and boot
  917. the default kernel. It is also possible to configure LILO to be able to boot one
  918. of several kernels, or even other operating systems than Linux, and it is
  919. possible for the user to choose which kernel or operating system is to be booted
  920. at boot time. LILO can be          ____     _____         _____
  921. configured so that if one holds down the |_alt|_, |_shift|_, or |_ctrl|_key at
  922. boot time (i.e.when LILO is loaded), LILO will ask what is to be booted and not
  923. boot the default right away. Alternatively, LILO can be configured so that it
  924. will always ask, with an optional timeout that will cause the default kernel to
  925. be booted.
  926.  
  927.  
  928.    The are other boot loaders than LILO. However, since LILO has been written
  929. especially for Linux, it has some features that are useful and that only it
  930. provides, for example the ability to pass arguments to the kernel at boot time,
  931. or overriding some configuration options built into the kernel. Hence, it is
  932. usually the best choice. Among the alternatives are bootlin and bootactv.3
  933.  
  934.  
  935.    Booting from floppy and from hard disk have both their advantages, but
  936. generally booting from the hard disk is nicer, since it avoids the hassle of
  937. playing around with floppies. It is also faster. However, it can be more
  938. troublesome to install the system so it can boot from the hard disk, so many
  939. people will first boot from floppy, then, when the system is otherwise installed
  940. and working well, will install LILO and start booting from the hard disk.
  941.  
  942.  
  943.    After the Linux kernel has been read into the memory, by whatever means, and
  944. is started for real, roughly the following things happen:
  945.  
  946. o The Linux kernel is installed compressed, so it will first uncompress itself.
  947. The beginning of the compressed kernel contains a small uncompressed program
  948. that does this.
  949.  
  950. o If you have a super-VGA card that Linux recognizes and that has some special
  951. text modes (such as 100 columns by 40 rows), Linux asks you which mode you want
  952. to use. During the kernel compilation, it is possible to preset a video mode, so
  953. that this is never asked. This can also be done with LILO or rdev(8).
  954. _____________________________3
  955.    I don't know much about any of the alternatives. If and when I learn, I will
  956. add more descriptions.
  957. 20                                        Chapter 3.  Boots And Shutdowns
  958.  
  959.  
  960.  
  961. oAfter this the kernel checks what other hardware there is (hard disks,
  962. floppies, network adapters: :):, and configures some of its device drivers
  963. appropriately; while it does this, it outputs messages about its findings. For
  964. example, when I boot, I it looks like this:
  965.  
  966.    LILO boot:
  967.    Loading linux.
  968.    Console: colour EGA+ 80x25, 8 virtual consoles
  969.    Serial driver version 3.94 with no serial options enabled
  970.    tty00 at 0x03f8 (irq = 4) is a 16450
  971.    tty01 at 0x02f8 (irq = 3) is a 16450
  972.    lp`init: lp1 exists (0), using polling driver
  973.    Memory: 7332k/8192k available (300k kernel code, 384k reserved, 176k data)
  974.    Floppy drive(s): fd0 is 1.44M, fd1 is 1.2M
  975.    Loopback device init
  976.    Warning WD8013 board not found at i/o = 280.
  977.    Math coprocessor using irq13 error reporting.
  978.    Partition check:
  979.    hda: hda1 hda2 hda3
  980.    VFS: Mounted root (ext filesystem).
  981.    Linux version 0.99.pl9-1 (root@haven) 05/01/93 14:12:20
  982.  
  983.  
  984.     The exact texts are different on different systems, depending on the
  985. hardware, the version of Linux being used, and how it has been configured.
  986.  
  987.    oThen the kernel will try to mount the root filesystem. The place is
  988. configurable at compilation time, or any time with rdev or LILO. The filesystem
  989. type is detected automatically. If the mounting of the root filesystem fails,
  990. for example because you didn't remember to include the corresponding filesystem
  991. driver in the kernel, the kernel panics and halts the system (there isn't much
  992. it can do, anyway).
  993.  
  994.     The root filesystem is usually mounted read-only (this can be set in the
  995. same way as the place).  This makes it possible to check the filesystem while it
  996. is mounted; it is not a good idea to check a filesystem that is mounted
  997. read-write.
  998.  
  999.  
  1000.    oAfter this, the kernel starts the program init(8) (located in /sbin/init) in
  1001. the background (this will always become process number 1). init does various
  1002. startup chores. The exact things it does depends on the version being used; see
  1003. chapter ?? for more information.
  1004.  
  1005.    oinit then starts a getty(8) for virtual consoles and serial lines. getty is
  1006. the program which lets people log in via virtual consoles and serial terminals.
  1007. init may also start some other programs, depending on how it is configured. 
  1008.  
  1009.    oAfter this, the boot is complete, and the system is up and running normally.
  1010.  
  1011. 3.3.  More about shutdowns                                                 21
  1012.  
  1013.  
  1014.  
  1015. 3.3   More about shutdowns
  1016.  
  1017.  
  1018.  
  1019. META: two different implemetnations of shutdown? one that uses reboot/halt as 
  1020. internal binaries that shouldn't be run by hand?
  1021.  
  1022.  
  1023.    It is important to follow the correct procedures when you shut down a Linux
  1024. system. If you fail do so, your filesystems probably will become trashed and the
  1025. files probably will become scrambled. This is because Linux has a disk cache
  1026. that won't write things to disk at once, but only at intervals. This greatly
  1027. improves performance but also means that if you just turn off the power at a
  1028. whim the cache may hold a lot of data and that what is on the disk may not be a
  1029. fully working filesystem (because only some things have been written to the
  1030. disk).
  1031.  
  1032.    Another reason against just flipping the power switch is that in a
  1033. multi-tasking system there can be lots of things going on in the background, and
  1034. shutting the power can be quite disastrous. This is especially true for machines
  1035. that several people use at the same time.
  1036.  
  1037.    The commands for properly shutting down a Linux system are shutdown(8) and 
  1038. halt(8) (both are located in /sbin). There are two usual ways of using them. 
  1039.  
  1040.    If you are running a system where you are the only user, the usual way of
  1041. using shutdown is to quit all running programs, log out on all virtual consoles,
  1042. log in as root on one of them (or stay logged in as root if you already are, but
  1043. you should change to the root directory, to avoid problems with unmounting),
  1044. then give the command halt or shutdown -h now (substitute now with a plus sign
  1045. and a number in minutes if you want a delay, though you usually don't on a
  1046. single user system) or halt.
  1047.  
  1048.    Alternatively, if your system has many users, use the command shutdown -h
  1049. +time message, where time is the time in minutes until the system is halted, and
  1050. message is a short explanation of why the system is shutting down.
  1051.  
  1052.     root   # shutdown -h +10 'We will install a new disk.  System should
  1053.     > be back on-line in three hours.'
  1054.  
  1055. This will warn everybody that the system will shut down in ten minutes, and that
  1056. they'd better get lost or loose data.  The warning is printed to every terminal
  1057. on which someone is logged in, including all xterms.
  1058.  
  1059.     Broadcast message from root (ttyp0) Wed Aug 2 01:03:25 1995...
  1060.     We will install a new disk.  System should
  1061. 22                                        Chapter 3.  Boots And Shutdowns
  1062.  
  1063.  
  1064.     be back on-line in three hours.
  1065.     The system is going DOWN for system halt in 10 minutes !!
  1066.  
  1067. The warning is automatically repeated a few times before the boot, with shorter
  1068. and shorter intervals as the time runs out. You can't use a delay with halt; it
  1069. is seldom appropriate to use halt on a multiuser system.
  1070.  
  1071.    META: /etc/inittab can give commands to execute when halting/rebooting
  1072.  
  1073.    When the real shutting down starts after any delays, all filesystems (except
  1074. the root one) are unmounted, user processes (if anybody is still logged in) are
  1075. killed, daemons are shut down, all filesystem are unmounted, and generally
  1076. everything settles down.
  1077.  
  1078.    When that is done, shutdown prints out a message that you can power down the
  1079. machine.  Then, and only then, should you move your fingers towards the power
  1080. switch.
  1081.  
  1082.    Sometimes, although rarely on any good system, it is impossible to shut down
  1083. properly.  For instance, if the kernel panics and crashes and burns and
  1084. generally misbehaves, it might be completely impossible to give any new
  1085. commands, henceshutting down properly is somewhat difficult, and just about
  1086. everything you can do is hope that nothing has been too severely damaged and
  1087. turn off the power. If the troubles are a bit less severe (say, somebody merely
  1088. hit your keyboard with an axe), and the kernel and the update program still run
  1089. normally, it is probably a good idea to wait a couple of minutes to give
  1090. update(8) a chance to flush the buffer cache, and only cut the power after that.
  1091.  
  1092.    Some people like to shut down using the command sync(8)4 three times, waiting
  1093. for the disk I/O to stop, then turn off the power. If there are no running
  1094. programs, this is about equivalent to using shutdown. However, it does not
  1095. unmount any filesystems and this can lead to problems with the ext2fs \clean
  1096. filesystem" flag. The triple-sync method is not recommended.
  1097.  
  1098.    (In case you're wondering: the reason for three syncs is that in the early
  1099. days of UNIX, when the commands were typed separately, that usually gave
  1100. sufficient time for most disk I/O to be finished.)
  1101.  
  1102. 3.4   Rebooting
  1103.  
  1104.  
  1105.  
  1106. Rebooting means booting the system again. This can be accomplished by first
  1107. shut-ting it down completely, turning power off, and then turning it back on. A
  1108. simpler
  1109. _____________________________4
  1110.    sync flushes the buffer cache.
  1111. 3.5.  Single user mode                                                     23
  1112.  
  1113.  
  1114.  
  1115. way is to ask shutdown to reboot the system, instead of merely halting it. This
  1116. is accomplished by using the -r option to shutdown, for example, by giving the
  1117. com-mand shutdown -r now. You can also use the reboot command (which, like halt,
  1118. doesn't wait until it perpetrates its foul deed).
  1119.  
  1120. 3.5   Single user mode
  1121.  
  1122. The shutdown command can also be used to bring the system down to single user
  1123. mode, in which no one can log in, but root can use the console. This is useful
  1124. for system administration tasks that can't be done while the system is running
  1125. normally.
  1126.  
  1127. Single user mode is discussed more thoroughly in chapter ??.
  1128.  
  1129. 3.6   Emergency boot floppies
  1130.  
  1131. It is not always possible to boot a computer from the hard disk. For example, if
  1132. you make a mistake in configuring LILO, you might make your system unbootable.
  1133. For these situations, you need an alternative way of booting that will always
  1134. work (as long as the hardware works). For typical PC's, this means booting from
  1135. the floppy drive.
  1136.  
  1137.  
  1138.    Most Linux distributions allow one to create an emergency boot floppy during
  1139. installation.  It is a good idea to do this.  However, many such boot disks
  1140. contain only the kernel, and assume you will be using the programs on the
  1141. distributions' installation disks to fix whatever problem you have. Sometimes
  1142. those programs aren't enough; for example, you might have to restore some files
  1143. from backups made with software not on the installation disks.
  1144.  
  1145.  
  1146.    Thus, it might be necessary to create a custom root floppy as well. The
  1147. Bootdisk HOWTO by Graham Chapman contains instructions for doing this.  You
  1148. must, of course, remember to keep your emergency boot and root floppies up to
  1149. date.
  1150.  
  1151.  
  1152.    You can't use the floppy drive you use to mount the root floppy for anything
  1153. else. This can be inconvenient if you only have one floppy. However, if you have
  1154. enough memory, you can configure your boot floppy to load the root disk to a
  1155. ramdisk (the boot floppy's kernel needs to be specially configured for this).
  1156. This frees the floppy drive after the root floppy has been loaded to a ramdisk.
  1157. 24                                        Chapter 3.  Boots And Shutdowns
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163. Chapter  4
  1164.  
  1165.  
  1166.  
  1167. Using  Disks  and  Other  Storage
  1168.  
  1169.  
  1170.  
  1171. Media
  1172.  
  1173.  
  1174.  
  1175.                                         On a clear disk you can seek forever.
  1176. When you install or upgrade your system, you need to do a fair amount of work on
  1177. your disks. You have to make filesystems on your disks so that files can be
  1178. stored on them and reserve space for the different parts of your system.
  1179.  
  1180.  
  1181.    This chapter explains all these initial activities. Usually, once you get
  1182. your system set up, you won't have to go through the work again, except for
  1183. using floppies. You'll need to come back to this chapter if you add a new disk
  1184. or want to fine-tune your disk usage.
  1185.  
  1186.  
  1187.    The basic tasks in administering disks are:
  1188.  
  1189.   o Format your disk. This does various things to prepare it for use, such as
  1190. checking for bad sectors. (Formatting is nowadays not necessary for most hard
  1191. disks.)
  1192.  
  1193.   o Partition a hard disk, if you want to use it for several activities that
  1194. aren't supposed to interfere with one another. One reason for partitioning is to
  1195. store different operating systems on the same disk.  Another reason is to keep
  1196. user files separate from system files, which simplifies back-ups and helps
  1197. protect the system files from corruption.
  1198.  
  1199.  
  1200.   o Make a filesystem (of a suitable type) on each disk or partition. The disk
  1201. means nothing to Linux until you make a filesystem; then files can be created
  1202. and accessed on it.
  1203.  
  1204.  
  1205.                                   25
  1206. 26                          Chapter 4.  Using Disks and Other Storage Media
  1207.  
  1208.  
  1209.  
  1210.    oMount different filesystems to form a single tree structure, either
  1211. automatically, or manually as needed. (Manually mounted filesystems usually need
  1212. to be un-mounted manually as well.)
  1213.  
  1214.    Chapter 6 contains information about virtual memory and disk caching, of
  1215. which you also need to be aware of when using disks.
  1216.  
  1217.    This chapter explains what you need to know for hard disks and floppies.
  1218. Unfortu-nately, because I lack the equipment, I cannot tell you much about using
  1219. other types of media, such as tapes or CD-ROM's.
  1220.  
  1221. 4.1   Two kinds of devices
  1222.  
  1223. UNIX, and therefore Linux, recognizes two different kinds of devices:
  1224. random-access block devices (such as disks), and character devices (such as
  1225. tapes and serial lines), some of which may be serial, and some random-access. 
  1226. Each supported device is represented in the filesystem as a device file. When
  1227. you read or write a device file, the data comes from or goes to the device it
  1228. represents. This way no special programs (and no special application programming
  1229. methodology, such as catching interrupts or polling a serial port) are necessary
  1230. to access devices; for example, to send a file to the printer, one could just
  1231. say 
  1232.  
  1233.     ttyp5 root " $ cat filename > /dev/lp1
  1234.     ttyp5 root " $
  1235.  
  1236. and the contents of the file are printed (the file must, of course, be in a form
  1237. that the printer understands). However, since it is not a good idea to have
  1238. several people cat their files to the printer at the same time, one usually uses
  1239. a special program to send the files to be printed (usually lpr(1)). This program
  1240. makes sure that only one file is being printed at a time, and will automatically
  1241. send files to the printer as soon as it finishes with the previous file.
  1242. Something similar is needed for most devices. In fact, one seldom needs to worry
  1243. about device files at all.
  1244.  
  1245.    Since devices show up as files in the filesystem (in the /dev directory), it
  1246. is easy to see just what device files exist, using ls(1) or another suitable
  1247. command. In the output of ls -l, the first column contains the type of the file
  1248. and its permissions. For example, inspecting a serial device gives on my system
  1249.  
  1250.     ttyp5 root " $ ls -l /dev/cua0
  1251.     crw-rw-rw-   1 root    uucp      5,  64 Nov 30  1993 /dev/cua0
  1252. 4.2.  Hard disks                                                         27
  1253.  
  1254.  
  1255.  
  1256.     ttyp5 root " $
  1257.  
  1258.  
  1259. The first character in the first column, i.e., `c' in crw-rw-rw- above, tells an
  1260. informed user the type of the file, in this case a character device. For
  1261. ordinary files, the first character is `-', for directories it is `d', and for
  1262. block devices `b'; see the ls(1) man page for further information.
  1263.  
  1264.    Note that usually all device files exist even though the device itself might
  1265. be not be installed.  So just because you have a file /dev/sda, it doesn't mean
  1266. that you really do have an SCSI hard disk. Having all the device files makes the
  1267. installation programs simpler, and makes it easier to add new hardware (there is
  1268. no need to find out the correct parameters for and create the device files for
  1269. the new device).
  1270.  
  1271. 4.2   Hard disks
  1272.  
  1273.  
  1274.  
  1275. This subsection introduces terminology related to hard disks.  If you already
  1276. know the terms and concepts, you can skip this subsection.
  1277.  
  1278.    See figure 4.1 for a schematic picture of the important parts in a hard disk.
  1279. A hard disk consists of one or more circular platters,1 of which either or both
  1280. surfaces are coated with a magnetic substance used for recording the data. For
  1281. each surface, there is a read-write head that examines or alters the recorded
  1282. data. The platters rotate on a common axis; a typical rotation speed is 3600
  1283. rotations per minute, although high-performance hard disks have higher speeds.
  1284. The heads move along the radius of the platters; this movement combined with the
  1285. rotation of the platters allows the head to access all parts of the surfaces.
  1286.  
  1287.    The processor (CPU) and the actual disk communicate through a disk
  1288. controller. This relieves the rest of the computer from knowing how to use the
  1289. drive, since the controllers for different types of disks can be made to use the
  1290. same interface towards the rest of the computer.  Therefore, the computer can
  1291. say just \hey disk, gimme what I want", instead of a long and complex series of
  1292. electric signals to move the head to the proper location and waiting for the
  1293. correct position to come under the head and doing all the other unpleasant stuff
  1294. necessary. (In reality, the interface to the controller is still complex, but
  1295. much less so than it would otherwise be.)  The controller can also do some other
  1296. stuff, such as caching, or automatic bad sector replacement.
  1297.  
  1298.    The above is usually what one needs to understand about the hardware. There
  1299. _____________________________1
  1300.    The platters are made of a hard substance, e.g., aluminium, which gives the
  1301. hard disk its name.
  1302. 28                          Chapter 4.  Using Disks and Other Storage Media
  1303.  
  1304.  
  1305.  
  1306. is also a bunch of other stuff, such as the motor that rotates the platters and
  1307. moves the heads, and the electronics that control the operation of the
  1308. mechanical parts, but that is mostly not relevant for understanding the working
  1309. principle of a hard disk. 
  1310.  
  1311.    The surfaces are usually divided into concentric rings, called tracks, and
  1312. these in turn are divided into sectors. This division is used to specify
  1313. locations on the hard disk and to allocate disk space to files. To find a given
  1314. place on the hard disk, one might say \surface 3, track 5, sector 7". Usually
  1315. the number of sectors is the same for all tracks, but some hard disks put more
  1316. sectors in outer tracks (all sectors are of the same physical size, so more of
  1317. them fit in the longer outer tracks). Typically, a sector will hold 512 bytes of
  1318. data. The disk itself can't handle smaller amounts of data than one sector.
  1319.  
  1320.                    Figure 4.1: A schematic picture of a hard disk.
  1321.  
  1322.  
  1323.  
  1324.    Each surface is divided into tracks (and sectors) in the same way. This means
  1325. that when the head for one surface is on a track, the heads for the other
  1326. surfaces are also on the corresponding tracks. All the corresponding tracks
  1327. taken together are called a cylinder. It takes time to move the heads from one
  1328. track (cylinder) to another, so by placing the data that is often accessed
  1329. together (say, a file) so that it is within one cylinder, it is not necessary to
  1330. move the heads to read all of it. This improves
  1331. 4.2.  Hard disks                                                         29
  1332.  
  1333.  
  1334.  
  1335. performance. It is not always possible to place files like this; files that are
  1336. stored in several places on the disk are called fragmented.
  1337.  
  1338.    The number of surfaces (or heads, which is the same thing), cylinders, and
  1339. sectors vary a lot; the specification of the number of each is called the
  1340. geometry of a hard disk. The geometry is usually stored in a special,
  1341. battery-powered memory location called the CMOS RAM, from where the operating
  1342. system can fetch it during bootup or driver initialization.
  1343.  
  1344.  
  1345.    Unfortunately, the BIOS2 has a design limitation, which makes it impossible
  1346. to specify a track number that is larger than 1024 in the CMOS RAM, which is too
  1347. little for a large hard disk. To overcome this, the hard disk controller lies
  1348. about the geometry, and translates the addresses given by the computer into
  1349. something that fits reality. For example, a hard disk might have 8 heads, 2048
  1350. tracks, and 35 sectors per track3. Its controller could lie to the computer and
  1351. claim that it has 16 heads, 1024 tracks, and 35 sectors per track, thus not
  1352. exceeding the limit on tracks, and translates the address that the computer
  1353. gives it by halving the head number, and doubling the track number. The math can
  1354. be more complicated in reality, because the numbers are not as nice as here (but
  1355. again, the details are not relevant for understanding the principle). This
  1356. translation distorts the operating system's view of how the disk is organized,
  1357. thus making it impractical to use the all-data-on-one-cylinder trick to boost
  1358. performance.
  1359.  
  1360.    The translation is only a problem for IDE disks. SCSI disks use a sequential
  1361. sector number (i.e., the controller translates a sequential sector number to
  1362. head/cylinder/sector), and a completely different method for the CPU to talk
  1363. with the controller, so they are insulated from the problem. Note, however, that
  1364. the computer might not know the real geometry of an SCSI disk either.
  1365.  
  1366.    Since Linux often will not know the real geometry of a disk, its filesystems
  1367. don't even try to keep files within a single cylinder. Instead, it tries to
  1368. assign sequentially numbered sectors to files, which almost always gives similar
  1369. performance. The issue is further complicated by on-controller caches, and
  1370. automatic prefetches done by the controller.
  1371.  
  1372.  
  1373.    Each hard disk is represented by a separate device file. There can (usually)
  1374. be only  two IDE hard disks. These are known as /dev/hda and /dev/hdb,
  1375. respectively. SCSI hard disks are known as /dev/sda, /dev/sdb, and so on.
  1376. Similar naming conventionsexist for other hard disk types Note that the device
  1377. files for the hard disks give access
  1378. _____________________________2
  1379.    The BIOS is some built-in software stored on ROM chips. It takes care, among
  1380. other things, of the initial stages
  1381. of booting.
  1382.   3The numbers are completely imaginary.
  1383. 30                          Chapter 4.  Using Disks and Other Storage Media
  1384.  
  1385.  
  1386.  
  1387. to the entire disk, with no regard to partitions (which will be discussed
  1388. below), and it's easy to mess up the partitions or the data in them if you
  1389. aren't careful. The disks' device files are usually used only to get access to
  1390. the master boot record (which will also be discussed below).
  1391.  
  1392.  
  1393. 4.3   Floppies
  1394. A floppy disk consists of a flexible membrane covered on one or both sides with
  1395. similar magnetic substance as a hard disk. The floppy disk itself doesn't have a
  1396. read-write head, that is included in the drive.  A floppy corresponds to one
  1397. platter in a hard disk, but is removable and one drive can be used to access
  1398. different floppies, whereas the hard disk is one indivisible unit.
  1399.  
  1400.    Like a hard disk, a floppy is divided into tracks and sectors (and the two
  1401. corre-sponding tracks on either side of a floppy form a cylinder), but there are
  1402. many fewer of them than on a hard disk.
  1403.  
  1404.    A floppy drive can usually use several different types of disks; for example,
  1405. a 31_2 inch drive can use both 720 kB and 1.44 MB disks. Since the drive has to
  1406. operate a bit differently and the operating system must know how big the disk
  1407. is, there are many device files for floppy drives, one per combination of drive
  1408. and disk type. Therefore, /dev/fd0H1440 is the first floppy drive (fd0), which
  1409. must be a 31_2inch drive, using a 31_2inch, high density disk (H) of size 1440
  1410. kB (1440), i.e., a normal 31_2inch HD floppy. For more information on the naming
  1411. conventions for the floppy devices.
  1412.  
  1413.    The names for floppy drives are complex, however, and Linux therefore has a
  1414. special floppy device type that automatically detects the type of the disk in
  1415. the drive. It works by trying to read the first sector of a newly inserted
  1416. floppy using different floppy types until it finds the correct one. This
  1417. naturally requires that the floppy is formatted first. The automatic devices are
  1418. called /dev/fd0, /dev/fd1, and so on.
  1419.  
  1420.    The parameters the automatic device uses to access a disk can also be set
  1421. using the program setfdprm(8). This can be useful if you need to use disks that
  1422. do not follow any usual floppy sizes, e.g., if they have an unusual number of
  1423. sectors, or if the autodetecting for some reason fails and the proper device
  1424. file is missing.
  1425.  
  1426.    Linux can handle many nonstandard floppy disk formats in addition to all the
  1427. standard ones. Some of these require using special formatting programs. We'll
  1428. skip these disk types for now.
  1429. 4.4.  Formatting                                                         31
  1430.  
  1431.  
  1432.  
  1433. 4.4   Formatting
  1434.  
  1435.  
  1436. Formatting is the process of writing marks on the magnetic media that are used
  1437. to mark tracks and sectors.  Before a disk is formatted, its magnetic surface is
  1438. a complete mess of magnetic signals. When it is formatted, some order is brought
  1439. into the chaos by essentially drawing lines where the tracks go, and where they
  1440. are divided into sectors. The actual details are not quite exactly like this,
  1441. but that is irrelevant. What is important, is that a disk cannot be used unless
  1442. it has been formatted. 
  1443.  
  1444.    The terminology is a bit confusing here: in MS-DOS, the word formatting is
  1445. used to cover also the process of creating a filesystem (which will be discussed
  1446. below). There, the two processes are often combined, especially for floppies.
  1447. When the distinction needs to be made, the real formatting is called low-level
  1448. formatting, while making the filesystem is called high-level formatting. In UNIX
  1449. circles, the two are called formatting and making a filesystem, so that's what
  1450. is used in this book as well.
  1451.  
  1452.    For IDE and some SCSI disks the formatting is actually done at the factory
  1453. and doesn't need to be repeated; hence most people rarely need to worry about
  1454. it.  In fact, formatting a hard disk can cause it to work less well, for example
  1455. because a disk might need to be formatted in some very special way to allow
  1456. automatic bad sector replacement to work.
  1457.  
  1458.    Disks that need or can be formatted, often require a special program anyway,
  1459. because the interface to the formatting logic inside the drive is different from
  1460. drive to drive. The formatting program is often either on the controller BIOS,
  1461. or is supplied as an MS-DOS program; neither of these can easily be used from
  1462. within Linux.
  1463.  
  1464.    During formatting one might encounter bad spots on the disk, called bad
  1465. blocks or bad sectors. These are sometimes handled by the drive itself, but even
  1466. then, if more of them develop, something needs to be done to avoid using those
  1467. parts of the disk. The logic to do this is built into the filesystem; how to add
  1468. the information into the filesystem is described below. Alternatively, one might
  1469. create a small partitionthat covers just the bad part of the disk; this approach
  1470. might be a good idea if the bad spot is very large, since filesystems can
  1471. sometimes have trouble with very large bad areas.
  1472.  
  1473.  
  1474.    Floppies are formatted with fdformat(8). The floppy device file to use is
  1475. given as the parameter. For example, the following command would format a high
  1476. density, 31_2inch floppy in the first floppy drive:
  1477.  
  1478.     ttyp5 root " $ fdformat /dev/fd0H1440
  1479.     Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.
  1480. 32                          Chapter 4.  Using Disks and Other Storage Media
  1481.  
  1482.  
  1483.  
  1484.     Formatting ... done
  1485.     Verifying ... done
  1486.     ttyp5 root " $
  1487.  
  1488.  
  1489. Note that if you want to use an autodetecting device (e.g., /dev/fd0), you must
  1490. set the parameters of the device with setfdprm(8) first. To achieve the same
  1491. effect as above, one would have to do the following:
  1492.  
  1493.     ttyp5 root " $ setfdprm /dev/fd0 1440/1440
  1494.     ttyp5 root " $ fdformat /dev/fd0
  1495.     Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.
  1496.     Formatting ... done
  1497.     Verifying ... done
  1498.     ttyp5 root " $
  1499.  
  1500. It is usually more convenient to choose the correct device file that matches the
  1501. type of the floppy. Note that it is unwise to format floppies to contain more
  1502. information than what they are designed for.
  1503.  
  1504.  
  1505.    fdformat will also validate the floppy, i.e., check it for bad blocks. It
  1506. will try a bad block several times (you can usually hear this, the drive noise
  1507. changes dramatically). If the floppy is only marginally bad (due to dirt on the
  1508. read/write head, some errors are false signals), fdformat won't complain, but a
  1509. real error will abort the validation process. The kernel will print log messages
  1510. for each I/O error it finds; these will go to the console or, if syslog is being
  1511. used, to the file /usr/adm/messages. fdformat itself won't tell where the error
  1512. is (one usually doesn't care, floppies are cheap enough that a bad one is
  1513. automatically thrown away).
  1514.  
  1515.     ttyp5 root " $ fdformat /dev/fd0H1440
  1516.     Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.
  1517.     Formatting ... done
  1518.     Verifying ... read: Unknown error
  1519.     ttyp5 root " $
  1520.  
  1521.  
  1522.    The badblocks(8) command can be used to search any disk or partition for bad
  1523. blocks (including a floppy). It does not format the disk, so it can be used to
  1524. check even existing filesystems. The example below checks a 31_2inch floppy with
  1525. two bad blocks.
  1526.  
  1527.     ttyp5 root " $ badblocks /dev/fd0H1440 1440
  1528.  
  1529.     718
  1530. 4.5.  Partitions                                                          33
  1531.  
  1532.  
  1533.  
  1534.     719
  1535.  
  1536.     ttyp5 root " $
  1537.  
  1538.  
  1539. badblocks outputs the block numbers of the bad blocks it finds. Most filesystems
  1540. can avoid such bad blocks. They maintain a list of known bad blocks, which is
  1541. initialized when the filesystem is made, and can be modified later. The initial
  1542. search for bad blocks can be done by the mkfs command (which initializes the
  1543. filesystem), but later checks should be done with badblocks and the new blocks
  1544. should be added with fsck. We'll describe mkfs and fsck later.
  1545.  
  1546. 4.5   Partitions
  1547.  
  1548. A hard disk can be divided into several partitions. Each partition functions as
  1549. if it were a separate hard disk. The idea is that if you have one hard disk, and
  1550. want to have, say, two operating systems on it, you can divide the disk into two
  1551. partitions. Each operating system uses its partition as it wishes and doesn't
  1552. touch the other one's. This way the two operating systems can co-exist
  1553. peacefully on the same hard disk. Without partitions one would have to buy a
  1554. hard disk for each operating system.
  1555.  
  1556.    Floppies are not partitioned. There is no technical reason against this, but
  1557. since they're so small, partitions would be useful only very rarely.
  1558.  
  1559. 4.5.1  The MBR, boot sectors and partition table
  1560.  
  1561. The information about how a hard disk has been partitioned is stored in its
  1562. first sector (that is, the first sector of the first track on the first disk
  1563. surface). The first sector is the master boot record (MBR) of the disk; this is
  1564. the sector that the BIOS reads in and starts when the machine is first booted.
  1565. The master boot record contains a small program that reads the partition table,
  1566. checks which partition is active (that is, marked bootable), and reads the first
  1567. sector of that partition, the partition's boot sector (the MBR is also a boot
  1568. sector, but it has a special status and therefore a special name). This boot
  1569. sector contains another small program that reads the first part of the operating
  1570. system stored on that partition (assuming it is bootable), and then starts it.
  1571.  
  1572.    The partitioning scheme is not built into the hardware, or even into the
  1573. BIOS. It is only a convention that many operating systems follow. Not all
  1574. operating systems do follow it, but they are the exceptions. Some operating
  1575. systems support partitions, but they occupy one partition on the hard disk, and
  1576. use their internal partitioning method
  1577. 34                          Chapter 4.  Using Disks and Other Storage Media
  1578.  
  1579.  
  1580.  
  1581. within that partition. The latter type exists peacefully with other operating
  1582. systems (including Linux), and does not require any special measures, but an
  1583. operating system that doesn't support partitions cannot co-exist on the same
  1584. disk with any other operating system.
  1585.  
  1586.  
  1587.    As a safety precaution, it is a good idea to write down the partition table
  1588. on a piece of paper, so that if it ever corrupts you don't have to lose all your
  1589. files. (A bad partition table can be fixed with fdisk).
  1590.  
  1591. 4.5.2  Extended and logical partitions
  1592.  
  1593.  
  1594. The original partitioning scheme for PC hard disks allowed only four partitions.
  1595. This quickly turned out to be too little in real life, partly because some
  1596. people want more than four operating systems (Linux, MS-DOS, OS/2, Minix,
  1597. FreeBSD, NetBSD, or Windows/NT, to name a few), but primarily because sometimes
  1598. it is a good idea to have several partitions for one operating system. For
  1599. example, swap space is usually best put in its own partition for Linux instead
  1600. of in the main Linux partition for reasons of speed (see below).
  1601.  
  1602.    To overcome this design problem, extended partitions were invented. This
  1603. trick allows partitioning a primary partition into sub-partitions. The primary
  1604. partition thus subdivided is the extended partition; the subpartitions are
  1605. logical partitions. They behave like primary4 partitions, but are created
  1606. differently.
  1607.  
  1608.    The partition structure of a hard disk might look like that in figure 4.2.
  1609. The disk is divided into three primary partitions, the second of which is
  1610. divided into two logical partitions. Part of the disk is not partitioned at all.
  1611. The disk as a whole and each primary partition has a boot sector.
  1612.  
  1613. 4.5.3  Partition types
  1614.  
  1615. The partition tables (the one in the MBR, and the ones for extended partitions)
  1616. contain one byte per partition that identifies the type of that partition. This
  1617. attempts to identify the operating system that uses the partition, or what it
  1618. uses it for. The purpose is to make it possible to avoid having two operating
  1619. systems accidentally using the same partition. However, in reality, operating
  1620. systems do not really care about the partition type byte; e.g., Linux doesn't
  1621. care at all what it is. Worse, some of them use it incorrectly; e.g., at least
  1622. some versions of DR-DOS ignore the most significant bit of the byte, while
  1623. others don't.
  1624. _____________________________4
  1625.    Illogical?
  1626. 4.5.  Partitions                                                          35
  1627.  
  1628.  
  1629.  
  1630.  
  1631.                    Figure 4.2: A sample hard disk partitioning.
  1632.  
  1633.    There is no standardization agency to specify what each byte value means, but
  1634. some commonly accepted ones are included in the table in table 4.1. The same
  1635. list is available in the Linux fdisk(8) program.
  1636.  
  1637.  
  1638.  
  1639.              Table 4.1: Partition types (from the Linux fdisk(8) program).
  1640.  
  1641.        _________________________________________________________
  1642.         0  Empty            40  Venix 80286   94  Amoeba BBT
  1643.         1  DOS 12-bit FAT   51  Novell?       a5  BSD/386
  1644.         2  XENIX root       52  Microport     b7  BSDI fs
  1645.         3  XENIX usr        63  GNU HURD      b8  BSDI swap
  1646.         4  DOS 16-bit <32M  64  Novell        c7  Syrinx
  1647.         5  Extended         75  PC/IX         db  CP/M
  1648.         6  DOS 16-bit  32M  80  Old MINIX     e1  DOS access
  1649.         7  OS/2 HPFS        81  Linux/MINIX   e3  DOS R/O
  1650.         8  AIX              82  Linux swap     f2 DOS secondary
  1651.         9  AIX bootable     83  Linux native   ff BBT
  1652.        _a__OS/2_Boot_Manag__93__Amoeba__________________________
  1653.  
  1654.  
  1655.  
  1656. 4.5.4  Partitioning a hard disk
  1657.  
  1658.  
  1659. There are many programs for creating and removing partitions.  Most operating
  1660. systems have their own, and it can be a good idea to use each operating system's
  1661. 36                          Chapter 4.  Using Disks and Other Storage Media
  1662.  
  1663.  
  1664.  
  1665. own, just in case it does something unusual that the others can't.  Many of the
  1666. programs are called fdisk, including the Linux one, or variations thereof.
  1667. Details on using the Linux fdisk are given on its man page. The cfdisk command
  1668. is similar to fdisk, but has a nicer (full screen) user interface.
  1669.  
  1670.    When using IDE disks, the boot partition (the partition with the bootable
  1671. kernel image files) must be completely within the first 1024 cylinders. This is
  1672. because the disk is used via the BIOS during boot (before the system goes into
  1673. protected mode), and BIOS can't handle more than 1024 cylinders. It is sometimes
  1674. possible to use a boot partition that is only partly within the first 1024
  1675. cylinders. This works as long as all the files that are read with the BIOS are
  1676. within the first 1024 cylinders. Since this is difficult to arrange, it is a
  1677. very bad idea to do it; you never know when a kernel update or disk
  1678. defragmentation will result in an unbootable system. Therefore, make sure your
  1679. boot partition is completely within the first 1024 cylinders.
  1680.  
  1681.  
  1682.    Some newer versions of the BIOS and IDE disks can, in fact, handle disks with
  1683. more than 1024 cylinders.  If you have such a system, you can forget about the
  1684. problem; if you aren't quite sure of it, put it within the first 1024 cylinders.
  1685.  
  1686.    Each partition should have an even number of sectors, since the Linux
  1687. filesystems use a 1 kB block size, i.e., two sectors. An odd number of sectors
  1688. will result in the last sector being unused. This won't result in any problems,
  1689. but it is ugly, and some versions of fdisk will warn about it.
  1690.  
  1691.    Changing a partition's size usually requires first backing up everything you
  1692. want to save from that partition (preferably the whole disk, just in case),
  1693. deleting the partition, creating new partition, then restoring everything to the
  1694. new partition. There is a program for MS-DOS, called fips, which does this
  1695. without requiring the backup and restore, but for other filesystems it is still
  1696. necessary.
  1697.  
  1698. 4.5.5  Device files and partitions
  1699.  
  1700. Each partition and extended partition has its own device file. The naming
  1701. convention for these files is that a partition's number is appended after the
  1702. name of the whole disk, with the convention that 1-4 are primary partitions
  1703. (regardless of how many primary partitions there are) and 5-8 are logical
  1704. partitions (regardless of within which primary partition they reside). For
  1705. example, /dev/hda1 is the first primary partition on the first IDE hard disk,
  1706. and /dev/sdb7 is the third extended partition on the second SCSI hard disk.
  1707. 4.6.  Filesystems                                                         37
  1708.  
  1709.  
  1710. 4.6   Filesystems
  1711.  
  1712. 4.6.1  What are filesystems?
  1713.  
  1714. A filesystem is the methods and data structures that an operating uses to keep
  1715. track of files on a disk or partition that is, the way the files are organized
  1716. on the disk. The word is also used to refer to a partition or disk that is used
  1717. to store the files or the type of the filesystem. Thus, one might say \I have
  1718. two filesystems" meaning one has two partitions on which one stores files, or
  1719. that one is using the \extended filesystem", meaning the type of the filesystem.
  1720.  
  1721.    The difference between a disk or partition and the filesystem it contains is
  1722. impor-tant. A few programs_including, reasonably enough, programs that create
  1723. filesystems_operate directly on the raw sectors of a disk or partition; if there
  1724. is an existing file system there it will be destroyed or seriously corrupted.
  1725. Most programs operate on a filesystem, and therefore won't work on a partition
  1726. that doesn't contain one (or that contains one of the wrong type).
  1727.  
  1728.    Before a partition or disk can be used as a filesystem, it needs to be
  1729. initialized, and the bookkeeping data structures need to be written to the disk.
  1730. This process is called making a filesystem.
  1731.  
  1732.    Most UNIX filesystem types have a similar general structure, although the
  1733. exact details vary quite a bit. The central concepts are superblock, inode, data
  1734. block, directory block, and indirection block.  The superblock contains
  1735. information about the filesystem as a whole, such as its size (the exact
  1736. information here depends on the filesystem). An inode contains all information
  1737. about a file, excepts its name. The name is stored in the directory, together
  1738. with the number of the inode.  A directory entry consists of a filename and the
  1739. number of the inode which represents the file. The inode contains the numbers of
  1740. several data blocks, which are used to store the data in the file. There is
  1741. space only for a few data block numbers in the inode, however, and if more are
  1742. needed, more space for pointers to the data blocks is allocated dynamically.
  1743. These dynamically allocated blocks are indirect blocks; the name indicates that
  1744. in order to find the data block, one has to find its number in the indirect
  1745. block first.
  1746.  
  1747.    UNIX filesystems usually allow one to create a hole in a file (this is done
  1748. with lseek(2); check the manual page), which means that the filesystem just
  1749. pretends that at a particular place in the file there is just zero bytes, but no
  1750. actual disk sectors are reserved for that place in the file (this means that the
  1751. file will use a bit less disk space). This happens especially often for small
  1752. binaries, Linux shared libraries, some
  1753. 38                          Chapter 4.  Using Disks and Other Storage Media
  1754.  
  1755.  
  1756. databases, and a few other special cases. (Holes are implemented by storing a
  1757. special value as the address of the data block in the indirect block or inode. 
  1758. This special address means that no data block is allocated for that part of the
  1759. file, ergo, there is a hole in the file.)
  1760.  
  1761.    Holes are moderately useful.  On the author's system, a simple measurement
  1762. showed a potential for about 4 MB of savings through holes of about 200 MB to-
  1763. tal used disk space. That system, however, contains relatively few programs and
  1764. no database files. The measurement tool is described in appendix B.
  1765.  
  1766. 4.6.2  Filesystems galore
  1767.  
  1768. Linux supports several types of filesystems. As of this writing the most
  1769. important ones are:
  1770.  
  1771.     minix       The oldest, presumed to be the most reliable, but quite lim-
  1772.                 ited in features (some time stamps are missing, at most 30
  1773.                 character filenames) and restricted in capabilities (at most
  1774.                 64 MB per filesystem).
  1775.  
  1776.     xia         A modified version of the minix filesystem that lifts the limits
  1777.                 on the filenames and filesystem sizes, but does not otherwise
  1778.                 introduce new features. It is not very popular, but is reported
  1779.                 to work very well.
  1780.  
  1781.     ext2        The most featureful of the native Linux filesystems, currently
  1782.                 also the most popular one. It is designed to be easily upwards
  1783.                 compatible, so that new versions of the filesystem code do not
  1784.                 require re-making the existing filesystems.
  1785.  
  1786.     ext         An older version of ext2 that wasn't upwards compatible. It
  1787.                 is hardly ever used in new installations any more, and most
  1788.                 people have converted to ext2.
  1789.  
  1790. In addition, support for several foreign filesystem exists, to make it easier to
  1791. exchange files with other operating systems.  These foreign filesystems work
  1792. just like native ones, except that they may be lacking in some usual UNIX
  1793. features, or have curiouslimitations, or other oddities.
  1794.  
  1795.     msdos       Compatibility with MS-DOS (and OS/2 and Windows NT)
  1796.                 FAT filesystems.
  1797. 4.6.  Filesystems                                                         39
  1798.  
  1799.  
  1800.     umsdos      Extends the msdos filesystem driver under Linux so that Linux
  1801.                 can see long filenames, owners, permissions, links, and device
  1802.                 files. This allows a normal msdos filesystem to be used as if
  1803.                 it were a Linux one, thus removing the need for a separate
  1804.                 partition for Linux.
  1805.  
  1806.  
  1807.     iso9660     The standard CD-ROM filesystem; the popular Rock Ridge
  1808.                 extension to the CD-ROM standard that allow longer file
  1809.                 names is supported automatically.
  1810.  
  1811.     nfs         A networked filesystem that allows sharing a filesystem be-
  1812.                 tween many computers to allow easy access to the files from
  1813.                 all of them.
  1814.  
  1815.     hpfs        The OS/2 filesystem.
  1816.  
  1817.     sysv        SystemV/386, Coherent, and Xenix filesystems.
  1818.  
  1819.  
  1820. META: ifs, userfs The choice of filesystem to use depends on the situation.  If
  1821. compatibility or other reasons make one of the non-native filesystems necessary,
  1822. then that one must be used. If one can choose freely, then it is probably wisest
  1823. to use ext2, since it has all the features but does not suffer from lack of
  1824. performance.
  1825.  
  1826.    There is also the proc filesystem, usually accessible as the /proc directory,
  1827. which is not really a filesystem at all, even though it looks like one. The proc
  1828. filesystem makes it easy to access certain kernel data structures, such as the
  1829. process list (hence the name). It makes these data structures look like a
  1830. filesystem, and that filesystem can be manipulated with all the usual file
  1831. tools. For example, to get a listing of all processes one might use the command
  1832.  
  1833.     ttyp5 root " $  ls -l /proc
  1834.     total 0
  1835.     dr-xr-xr-x   4 root    root         0 Jan 31 20:37 1
  1836.     dr-xr-xr-x   4 liw     users         0 Jan 31 20:37 63
  1837.     dr-xr-xr-x   4 liw     users         0 Jan 31 20:37 94
  1838.     dr-xr-xr-x   4 liw     users         0 Jan 31 20:37 95
  1839.     dr-xr-xr-x   4 root    users         0 Jan 31 20:37 98
  1840.     dr-xr-xr-x   4 liw     users         0 Jan 31 20:37 99
  1841.     -r--r--r--   1 root    root         0 Jan 31 20:37 devices
  1842.     -r--r--r--   1 root    root         0 Jan 31 20:37 dma
  1843.     -r--r--r--   1 root    root         0 Jan 31 20:37 filesystems
  1844. 40                          Chapter 4.  Using Disks and Other Storage Media
  1845.  
  1846.  
  1847.     -r--r--r--   1 root    root         0 Jan 31 20:37 interrupts
  1848.     -r--------   1 root    root     8654848 Jan 31 20:37 kcore
  1849.     -r--r--r--   1 root    root         0 Jan 31 11:50 kmsg
  1850.     -r--r--r--   1 root    root         0 Jan 31 20:37 ksyms
  1851.     -r--r--r--   1 root    root         0 Jan 31 11:51 loadavg
  1852.     -r--r--r--   1 root    root         0 Jan 31 20:37 meminfo
  1853.     -r--r--r--   1 root    root         0 Jan 31 20:37 modules
  1854.     dr-xr-xr-x   2 root    root         0 Jan 31 20:37 net
  1855.     dr-xr-xr-x   4 root    root         0 Jan 31 20:37 self
  1856.     -r--r--r--   1 root    root         0 Jan 31 20:37 stat
  1857.     -r--r--r--   1 root    root         0 Jan 31 20:37 uptime
  1858.     -r--r--r--   1 root    root         0 Jan 31 20:37 version
  1859.     ttyp5 root " $
  1860.  
  1861.  
  1862.  
  1863. (There will be a few extra files that don't correspond to processes, though. The
  1864. above example has been shortened.)
  1865.  
  1866.    Note that even though it is called a filesystem, no part of the proc
  1867. filesystem touches any disk. It exists only in the kernel's imagination.
  1868. Whenever anyone tries to look at any part of the proc filesystem, the kernel
  1869. makes it look as if the part existed somewhere, even though it doesn't. So, even
  1870. though there is a multi-megabyte /proc/kmem file, it doesn't take any disk
  1871. space.
  1872.  
  1873. 4.6.3  Which filesystem should be used?
  1874.  
  1875. There is usually little point in using many different filesystems.  Currently,
  1876. ext2fs is the most popular one, and it is probably the wisest choice.  Depending
  1877. on the overhead for bookkeeping structures, speed, (perceived) reliability,
  1878. compatibility, and various other reasons, it may be advisable to use another
  1879. file system. This needs to be decided on a case-by-case basis.
  1880.  
  1881. 4.6.4  Creating a filesystem
  1882.  
  1883. Filesystems are created, i.e., initialized, with the mkfs(8) command. There is
  1884. actually a separate program for each filesystem type. mkfs is just a front end
  1885. that runs the appropriate program depending on the desired filesystem type. The
  1886. type is selected with the -t fstype option.
  1887.  
  1888.    The programs called by mkfs have slightly different command line interfaces.
  1889. The 
  1890. 4.6.  Filesystems                                                         41
  1891.  
  1892. common and most important options are summarized below; see the manual pages for
  1893. more.
  1894.  
  1895.     -t fstypeSelect the type of the filesystem.
  1896.     -c Search bad bad blocks and initialize the bad block list accordingly.
  1897.     -l filenameRead the initial bad block list from the file filename.
  1898.  
  1899. To create an ext2 filesystem on a floppy, one would give the following commands:
  1900.  
  1901.     ttyp6 root " $ fdformat -n /dev/fd0H1440
  1902.     Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.
  1903.     Formatting ... done
  1904.     ttyp6 root " $ badblocks /dev/fd0H1440 1440 > bad-blocks
  1905.     ttyp6 root " $ mkfs -t ext2 -l bad-blocks /dev/fd0H1440
  1906.     mke2fs 0.5a, 5-Apr-94 for EXT2 FS 0.5, 94/03/10
  1907.     360 inodes, 1440 blocks
  1908.     72 blocks (5.00%) reserved for the super user
  1909.     First data block=1
  1910.     Block size=1024 (log=0)
  1911.     Fragment size=1024 (log=0)
  1912.     1 block group
  1913.     8192 blocks per group, 8192 fragments per group
  1914.     360 inodes per group
  1915.  
  1916.  
  1917.     Writing inode tables: done
  1918.     Writing superblocks and filesystem accounting information: done
  1919.     ttyp6 root " $
  1920.  
  1921.  
  1922.  
  1923. First, the floppy was formatted (the -n option prevents validation, i.e., bad
  1924. block checking). Then bad blocks were searched with badblocks, with the output
  1925. redirected to a file, bad-blocks.  Finally, the filesystem was created, with the
  1926. bad block list initialized by whatever badblocks found.
  1927.  
  1928.    The -c option could have been used with mkfs instead of badblocks and a
  1929. separate file. The example below does that.
  1930.  
  1931.     ttyp6 root " $ mkfs -t ext2 -c /dev/fd0H1440
  1932.     mke2fs 0.5a, 5-Apr-94 for EXT2 FS 0.5, 94/03/10
  1933.     360 inodes, 1440 blocks
  1934. 42                          Chapter 4.  Using Disks and Other Storage Media
  1935.  
  1936.  
  1937.     72 blocks (5.00%) reserved for the super user
  1938.     First data block=1
  1939.     Block size=1024 (log=0)
  1940.     Fragment size=1024 (log=0)
  1941.     1 block group
  1942.     8192 blocks per group, 8192 fragments per group
  1943.     360 inodes per group
  1944.  
  1945.  
  1946.     Checking for bad blocks (read-only test): done
  1947.     Writing inode tables: done
  1948.     Writing superblocks and filesystem accounting information: done
  1949.     ttyp6 root " $
  1950.  
  1951.  
  1952.  
  1953. The -c is more convenient than a separate use of badblocks, but badblocks is 
  1954. necessary for checking after the filesystem has been created.
  1955.  
  1956.    The process to prepare to filesystems on hard disks or partitions is the same
  1957. as for floppies, except that the formatting isn't needed.
  1958.  
  1959. 4.6.5  Mounting and unmounting
  1960.  
  1961. Before one can use a filesystem, it has to be mounted. The operating system then
  1962. does various bookkeeping things to make sure that everything works. Since all
  1963. files in UNIX are in a single directory tree, the mount operation will make it
  1964. look like the contents of the new filesystem are the contents of an existing
  1965. subdirectory in some already mounted filesystem.
  1966.  
  1967.  
  1968.    For example, figure 4.3 shows three separate filesystems, each with their own
  1969. root directory. When the last two filesystems are mounted below /home and /usr,
  1970. respec-tively, on the first filesystem, we can get a single directory tree, as
  1971. in figure 4.4.
  1972.  
  1973.  
  1974.  
  1975.                 /                            /                  /
  1976.  ---------------|---------------         ----|------       -----|------
  1977.  |      |    |       |   |     |         |   |     |       |     |    |
  1978.  |      |    |       |   |     |         |   |     |       |     |    |
  1979.  |      |    |       |   |     |         |   |     |       |     |    |
  1980.  |      |    |       |   |     |         |   |     |       |     |    |
  1981. cin   dev   home    etc lib   usr       abc liw   ftp     bin   etc  lib
  1982.                       Figure 4.3: Three separate filesystems.
  1983.  
  1984.  
  1985.  
  1986.    The mounts could be done as in the following example:
  1987. 4.6.  Filesystems                                                         43
  1988.  
  1989.  
  1990.  
  1991.                                /                
  1992.                 ---------------|---------------  
  1993.                 |      |    |       |   |     |  
  1994.                 |      |    |       |   |     |  
  1995.                 |      |    |       |   |     |  
  1996.                 |      |    |       |   |     |  
  1997.                bin   dev   home    etc lib   usr 
  1998.                             |                 |
  1999.                         ----|------      -----|------
  2000.                         |   |     |      |     |    |
  2001.                         |   |     |      |     |    |
  2002.                         |   |     |      |     |    |
  2003.                         |   |     |      |     |    |
  2004.                        abc liw   ftp    bin   etc  lib
  2005.                   Figure 4.4: /home and /usr have been mounted.
  2006.  
  2007.     ttyp6 root " $  mount /dev/hda2 /home
  2008.     ttyp6 root " $  mount /dev/hda3 /usr
  2009.     ttyp6 root " $
  2010.  
  2011. The mount(8) command takes two arguments.  The first one is the device file
  2012. cor-responding to the disk or partition containing the filesystem. The second
  2013. one is the directory below which it will be mounted. After these commands the
  2014. contents of the two filesystems look just like the contents of the /home and
  2015. /usr directories, respec-tively. One would then say that \/dev/hda2 is mounted
  2016. on /home", and similarly for /usr. To look at either filesystem, one would look
  2017. at the contents of the directory on which it has been mounted, just as it were
  2018. any other directory. Note the difference between the device file, /dev/hda2, and
  2019. the mounted-on directory, /home. The device file gives access to the raw
  2020. contents of the disk, the mounted-on directory gives access to the files on the
  2021. disk. The mounted-on directory is called the mount point.
  2022.  
  2023.    The mounted-on directory need not be empty, although it must exist. Any files
  2024. in it, however, will be inaccessible by name while the filesystem is mounted.
  2025. (Any files that have already been opened will still be accessible. Files that
  2026. have hard links from other directories can be accessed using those names.) There
  2027. is no harm done with this, and it can even be useful.  For instance, some people
  2028. like to have /tmp and /usr/tmp synonymous, and make /tmp be a symbolic link to
  2029. /usr/tmp. When the system is booted, before the /usr filesystem is mounted, a
  2030. /usr/tmp directory residing on the root filesystem is used instead. When /usr is
  2031. mounted, it will make the /usr/tmp directory on the root filesystem
  2032. inaccessible. If /usr/tmp didn't exist on the root filesystem, it would be
  2033. impossible to use temporary files before mounting /usr.
  2034.  
  2035.    If you don't intend to write anything to the filesystem, use the -r switch
  2036. for mount
  2037. 44                          Chapter 4.  Using Disks and Other Storage Media
  2038.  
  2039.  
  2040.  
  2041. to do a readonly mount. This will make the kernel stop any attempts at writing
  2042. to the filesystem, and will also stop the kernel from updating file access times
  2043. in the inodes. Read-only mounts are necessary for unwritable media, e.g.,
  2044. CD-ROM's.
  2045.  
  2046.    The alert reader has already noticed a slight logistical problem.  How is the
  2047. first filesystem (called the root filesystem, because it contains the root
  2048. directory) mounted, since it obviously can't be mounted on another filesystem?
  2049. Well, the answer is that it is done by magic.5 The root filesystem is magically
  2050. mounted at boot time, and one can rely on it to always be mounted_if the root
  2051. filesystem can't be mounted, the system does not boot. The name of the
  2052. filesystem that is magically mounted as root is either compiled into the kernel,
  2053. or set using LILO or rdev.
  2054.  
  2055.  
  2056.    The root filesystem is usually first mounted readonly. The startup scripts
  2057. will then run fsck(8) to verify its validity, and if there are no problems, they
  2058. will re-mount it so that writes will also be allowed. fsck must not be run on a
  2059. mounted filesystem, since any changes to the filesystem while fsck is running
  2060. will cause trouble. Since the root filesystem is mounted readonly while it is
  2061. being checked, fsck can fix any problems without worry, since the remount
  2062. operation will flush any metadata that the filesystem keeps in memory.
  2063.  
  2064.    On many systems there are other filesystems that should also be mounted auto-
  2065. matically at boot time. These are specified in the /etc/fstab file; see the
  2066. fstab(5) man page for details on the format. The details of exactly when the
  2067. extra filesystems are mounted depend on many factors, and can be configured by
  2068. each administrator if need be. When the chapter on booting is finished, you may
  2069. read all about it there.
  2070.  
  2071.    When a filesystem no longer needs to be mounted, it can be unmounted with
  2072. umount(8)6. umount takes one argument: either the device file or the mount
  2073. point. For example, to unmount the directories of the previous example, one
  2074. could use the commands
  2075.  
  2076.     ttyp6 root " $  umount /dev/hda2
  2077.     ttyp6 root " $  umount /usr
  2078.     ttyp6 root " $
  2079.  
  2080. See the man page for further instructions on how to use the command. It is
  2081. imperative that you always unmount a mounted floppy. Don't just pop the floppy
  2082. out of the drive! Because of disk caching, the data is not necessarily written
  2083. to the floppy until you unmount it, so removing the floppy from the drive too
  2084. early might cause the contents
  2085. _____________________________
  2086. 5    For more information, see the kernel source or the Kernel Hackers' Guide.
  2087. 6    It should of course be unmount(8), but the n mysteriously disappeared in
  2088. the 70's, and hasn't been seen since. Please return it to Bell Labs, NJ, if you
  2089. find it.
  2090. 4.6.  Filesystems                                                         45
  2091.  
  2092.  
  2093.  
  2094. to become garbled. If you just read from the floppy, this is not very likely,
  2095. but if you write, even accidentally, the result may be catastrophic.
  2096.  
  2097.    Mounting and unmounting requires super user priviledges, i.e., only root can
  2098. do it. The reason for this is that if any user can mount a floppy on any
  2099. directory, then it is rather easy to create a floppy with, say, a Trojan horse
  2100. disguised as /bin/sh, or any other often used program. However, it is often
  2101. necessary to allow users to use floppies, and there are several ways to do this:
  2102.  
  2103.  
  2104.   o Give the users the root password.  This is obviously bad security, but is
  2105. the easiest solution. It works well if there is no need for security anyway,
  2106. which is the case on many non-networked, personal systems.
  2107.  
  2108.   o Use a program such as sudo(8) to allow users to use mount. This is still bad
  2109. security, but doesn't directly give super user priviledges to everyone.7
  2110.  
  2111.   o Make the users use mtools, a package for manipulating MS-DOS filesystems,
  2112. without mounting them. This works well if MS-DOS floppies are the only thing
  2113. that is needed, but is rather awkward otherwise.
  2114.  
  2115.   o List the floppy devices and their allowable mount points together with the
  2116. suit-able options in /etc/fstab.
  2117.  
  2118. The last alternative can be implemented by adding a line like the following to
  2119. /etc/fstab:
  2120.  
  2121.     /dev/fd0 /floppy msdos user,noauto
  2122.  
  2123. The columns are: device file to mount, directory to mount on, filesystem type,
  2124. and options.  The noauto option stops this mount to be done automatically when
  2125. the system is started (i.e., it stops mount -a from mounting it). The user
  2126. option allows any user to mount the filesystem, and, because of security
  2127. reasons, disallows execution of programs (normal or setuid) and interpretation
  2128. of device files from the mounted filesystem. After this, any user can mount a
  2129. floppy with an msdos filesystem with the following command:
  2130.  
  2131.     ttyp6 root " $  mount /floppy
  2132.     ttyp6 root " $
  2133.  
  2134. The floppy can (and needs to, of course) be unmounted with the corresponding
  2135. umount command.
  2136.  
  2137.  
  2138.    META: What to do if several types of floppies are needed?
  2139. _____________________________
  2140. 7  It requires several seconds of hard thinking on the users' behalf.
  2141. 46                          Chapter 4.  Using Disks and Other Storage Media
  2142.  
  2143.  
  2144.  
  2145. 4.6.6  Keeping filesystems healthy
  2146.  
  2147. Filesystems are complex creatures, and as such, they tend to be somewhat
  2148. error-prone. A filesystem's correctness and validity can be checked using the
  2149. fsck(8) command. It can be instructed to repair any minor problems it finds, and
  2150. to alert the user if there any unrepairable problems. Fortunately, the code to
  2151. implement filesystems is debugged quite effectively, so there are seldom any
  2152. problems at all, and they are usually caused by power failures, failing
  2153. hardware, or operator errors; for example, by not shutting down the system
  2154. properly.
  2155.  
  2156.    Most systems are setup to run fsck automatically at boot time, so that any
  2157. errors are detected (and hopefully corrected) before the system is used. Use of
  2158. a corrupted filesystem tends to make things worse: if the data structures are
  2159. messed up, using the filesystem will probably mess them up even more, resulting
  2160. in more data loss. However, fsck can take a while to run on big filesystems, and
  2161. since errors almost never occur if the system has been shut down properly, a
  2162. couple of tricks are used to avoid doing the checks in such cases. The first is
  2163. that if the file /etc/fastboot exists, no checks are made. The second is that
  2164. the ext2 filesystem has a special marker in its superblock that tells whether
  2165. the filesystem was unmounted properly after the previous mount.  This allows
  2166. e2fsck (the version of fsck for the ext2 filesystem) to avoid checking the
  2167. filesystem if the flag indicates that the unmount was done (the assumption being
  2168. that a proper unmount indicates no problems). Whether the /etc/fastboot trick
  2169. works on your system depends on your startup scripts, but the ext2 trick works
  2170. every time you use e2fsck_it has to be explicitly bypassed with an option to
  2171. e2fsck to be avoided. (See the e2fsck(8) man page for details on how.)
  2172.  
  2173.    The automatic checking only works for the filesystems that are mounted
  2174. automat-ically at boot time. Use fsck manually to check other filesystems, e.g.,
  2175. floppies.
  2176.  
  2177.    If fsck finds unrepairable problems, you need either in-depth knowlege of how
  2178. filesystems work in general, and the type of the corrupt filesystem in
  2179. articular, or good backups. The latter is easy (although sometimes tedious) to
  2180. arrange, the former can sometimes be arranged via a friend, the Linux newsgroups
  2181. and mailing lists, or some other source of support, if you don't have the
  2182. know-how yourself. I'd like to tell you more about it, but my lack of education
  2183. and experience in this regard hinders me. The debugfs(8) program by Theodore
  2184. T'so should be useful.
  2185.  
  2186.    fsck must only be run on unmounted filesystems, never on mounted filesystems
  2187. (with the exception of the read-only root during startup). This is because it
  2188. accesses the raw disk, and can therefore modify the filesystem without the
  2189. operating system realizing it. There will be trouble, if the operating system is
  2190. confused.
  2191. 4.7.  Disks without filesystems                                               47
  2192.  
  2193.  
  2194.  
  2195.    It can be a good idea to periodically check for bad blocks. This is done with
  2196. the badblocks command. It outputs a list of the numbers of all bad blocks it can
  2197. find. This list can be fed to fsck to be recorded in the filesystem data
  2198. structures so that the operating system won't try to use the bad blocks for
  2199. storing data. The following example will show how this could be done.
  2200.  
  2201.     ttyp6 root " $ badblocks /dev/fd0H1440 1440 > bad-blocks
  2202.     ttyp6 root " $ fsck -t ext2 -l bad-blocks /dev/fd0H1440
  2203.     Parallelizing fsck version 0.5a (5-Apr-94)
  2204.     e2fsck 0.5a, 5-Apr-94 for EXT2 FS 0.5, 94/03/10
  2205.     Pass 1: Checking inodes, blocks, and sizes
  2206.     Pass 2: Checking directory structure
  2207.     Pass 3: Checking directory connectivity
  2208.     Pass 4: Check reference counts.
  2209.     Pass 5: Checking group summary information.
  2210.  
  2211.     /dev/fd0H1440: ***** FILE SYSTEM WAS MODIFIED *****
  2212.     /dev/fd0H1440: 11/360 files, 63/1440 blocks
  2213.     ttyp6 root " $
  2214.  
  2215. 4.7   Disks without filesystems
  2216.  
  2217.  Not all disks or partitions are used as filesystems. A swap partition, for
  2218. example, will not have a filesystem on it. Many floppies are used in a
  2219. tape-drive emulating fashion, so that a tar or other file is written directly on
  2220. the raw disk, without a filesystem. This has the advantages of making more of
  2221. the disk usable (a filesystem always has some bookkeeping overhead) and more
  2222. easily compatible with other systems: the tar file format is the same on all
  2223. systems, while filesystems are different on most systems. You will quickly get
  2224. used to disks without filesystems if you need them.  Bootable Linux floppies
  2225. also do not necessarily have a filesystem, although that is also possible.
  2226.  
  2227.  
  2228.    One reason to use raw disks is to make image copies of them. For instance, if
  2229. the disk contains a partially damaged filesystem, it is a good idea to make an
  2230. exact copy of it before trying to fix it, since then you can start again if your
  2231. fixing breaks things even more. One way to do this is to use dd(1): 
  2232.  
  2233.     ttyp2 root /usr/tmp $  dd if=/dev/fd0H1440 of=floppy-image
  2234.  
  2235.     2880+0 records in
  2236. 48                          Chapter 4.  Using Disks and Other Storage Media
  2237.  
  2238.  
  2239.  
  2240.     2880+0 records out
  2241.     ttyp2 root /usr/tmp $  dd if=floppy-image of=/dev/fd0H1440
  2242.     2880+0 records in
  2243.     2880+0 records out
  2244.     ttyp2 root /usr/tmp $
  2245.  
  2246. The first dd makes an exact image of the floppy to the file floppy-image, the
  2247. second one writes the image to the floppy.  (The user has presumably switched
  2248. the floppy before the second command. Otherwise the command pair is of doubtful
  2249. usefulness.)
  2250.  
  2251.  
  2252. 4.8   Allocating disk space
  2253.  
  2254. 4.8.1  Partitioning schemes
  2255.  
  2256. It is not easy to partition a disk in the best possible way. Worse, there is no
  2257. universally correct way to do it; there are too many factors involved.
  2258.  
  2259.    The traditional way is to have a (relatively) small root filesystem, which
  2260. contains /bin, /etc, /dev, /lib, /tmp, and other stuff that is needed to get the
  2261. system up and running.  This way, the root filesystem (in its own partition or
  2262. on its own disk) is all that is needed to bring up the system. The reasoning is
  2263. that if the root filesystem is small and is not heavily used, it is less likely
  2264. to become corrupt when the system crashes, and you will therefore find it easier
  2265. to fix any problems caused by the crash. Then you create separate partitions or
  2266. use separate disks for the directory tree below /usr, the users' home
  2267. directories (often under /home), and the swap space. Separating the home
  2268. directories (with the users' files) in their own partition makes backups easier,
  2269. since it is usually not necessary to backup programs (which reside below /usr). 
  2270. In a networked environment it is also possible to share /usr among several
  2271. machines (e.g., by using NFS), thereby reducing the total disk space required by
  2272. several tens or hundreds of megabytes times the number of machines.
  2273.  
  2274.    The problem with having many partitions is that it splits the total amount of
  2275. free disk space into many small pieces. Nowadays, when disks and (hopefully)
  2276. operating systems are more reliable, many people prefer to have just one
  2277. partition that holds all their files. On the other hand, it can be less painful
  2278. to back up (and restore) a small partition.
  2279.  
  2280.    For a small hard disk (assuming you don't do kernel development), the best
  2281. way to go is probably to have just one partition. For large hard disks, it is
  2282. probably better to have a few large partitions, just in case something does go
  2283. wrong. (Note that `small'
  2284. 4.8.  Allocating disk space                                                  49
  2285.  
  2286.  
  2287.  
  2288. and `large' are used in a relative sense here; your needs for disk space decide
  2289. what the threshold is.)
  2290.  
  2291.    If you have several disks, you might wish to have the root filesystem
  2292. (including /usr) on one, and the users' home directories on another.
  2293.  
  2294.    It is a good idea to be prepared to experiment a bit with different
  2295. partitioning schemes (over time, not just while first installing the system).
  2296. This is a bit of work, since it essentially requires you to install the system
  2297. from scratch several times, but it is the only way to be sure you do it right.
  2298.  
  2299. 4.8.2  Space requirements
  2300.  
  2301.  
  2302. The Linux distribution you install will give some indication of how much disk
  2303. space you need for various configurations. Programs installed separately may
  2304. also do the same. This will help you plan your disk space usage, but you should
  2305. prepare for the future and reserve some extra space for things you will notice
  2306. later that you need.
  2307.  
  2308.    The amount you need for user files depends on what your users wish to do.
  2309. Most people seem to need as much space for their files as possible, but the
  2310. amount they will live happily with varies a lot. Some people do only light text
  2311. processing and will survive nicely with a few megabytes, others do heavy image
  2312. processing and will need gigabytes. 
  2313.    By the way, when comparing file sizes given in kilobytes or megabytes and
  2314. disk space given in megabytes, it can be important to know that the two units
  2315. can be different. Some disk manufacturers like to pretend that a kilobyte is
  2316. 1000 bytes and a megabyte is 1000 kilobytes, while all the rest of the computing
  2317. world uses 1024 for both factors. Therefore, my 345 MB hard disk is really a 330
  2318. MB hard disk.8
  2319.  
  2320.  
  2321.    Swap space allocation is discusses in section 6.5.
  2322.  
  2323. 4.8.3  Examples of hard disk allocation
  2324.  
  2325.  
  2326. I used to have a 109 MB hard disk. Now I am using a 330 MB hard disk. I'll
  2327. explain how and why I partitioned these disks. 
  2328.    The 109 MB disk I partitioned in a lot of ways, when my needs and the
  2329. operating systems I used changed; I'll explain two typical scenarios. First, I
  2330. used to run MS- DOS together with Linux.  For that, I needed about 20 MB of hard
  2331. disk, or just
  2332. _____________________________
  2333. 8    Sic transit discus mundi.
  2334. 50                          Chapter 4.  Using Disks and Other Storage Media
  2335.  
  2336.  
  2337.  
  2338. enough to have MS-DOS, a C compiler, an editor, a few other utilities, the
  2339. program I was working on, and enough free disk space to not feel claustrophobic.
  2340. For Linux, I had a 10 MB swap partition, and the rest, or 79 MB, was a single
  2341. partition with all the files I had under Linux. I experimented with having
  2342. separate root, /usr, and /home partitions, but there was never enough free disk
  2343. space in one piece to do much interesting.
  2344.  
  2345.    When I didn't need MS-DOS anymore, I repartitioned the disk so that I had a
  2346. 12 MB swap partition, and again had the rest as a single filesystem.
  2347.  
  2348.    The 330 MB disk is partitioned into several partitions, like this:
  2349.  
  2350.        5 MB   root filesystem
  2351.       10 MB   swap partition
  2352.      180 MB   /usr filesystem
  2353.      120 MB   /home filesystem
  2354.       15 MB   scratch partition
  2355.  
  2356.  
  2357. The scratch partition is for playing around with things that require their own
  2358. par-tition, e.g., trying different Linux distributions, or comparing speeds of
  2359. filesystems. When not needed for anything else, it is used as swap space (I like
  2360. to have a lot of open windows).
  2361.  
  2362. 4.8.4  Adding more disk space for Linux
  2363.  
  2364. Adding more disk space for Linux is easy, at least after the hardware has been
  2365. properly installed (the hardware installation is outside the scope of this
  2366. book).  You format it if necessary, then create the partitions and filesystem as
  2367. described above, and add the proper lines to /etc/fstab so that it is mounted
  2368. automatically.
  2369.  
  2370. 4.8.5  Tips for saving disk space
  2371.  
  2372. The best tip for saving disk space is to avoid installing unnecessary programs.
  2373. Most Linux distributions have an option to install only part of the packages
  2374. they contain, and by analyzing your needs you might notice that you don't need
  2375. most of them. This will help save a lot of disk space, since many programs are
  2376. quite large. Even if you do need a particular package or program, you might not
  2377. need all of it. For example, some on-line documentation might be unnecessary, as
  2378. might some of the Elisp files for GNU Emacs, some of the fonts for X11, or some
  2379. of the libraries for programming.
  2380. 4.8.  Allocating disk space                                                  51
  2381.  
  2382.    If you cannot uninstall packages, you might look into compression.
  2383. Compression programs such as gzip(1) or zip(1) will compress (and uncompress)
  2384. individual files or groups of files. The gzexe system will compress and
  2385. uncompress programs invisibly to the user (unused programs are compressed, then
  2386. uncompressed as they are used). The experimental DouBle system will compress all
  2387. files in a filesystem, invisibly to the programs that use them. (If you are
  2388. familiar with products such as Stacker for MS-DOS, the principle is the same.)
  2389. 52                          Chapter 4.  Using Disks and Other Storage Media
  2390.  
  2391.  
  2392.  
  2393.  
  2394.  
  2395. Chapter  5
  2396.  
  2397.  
  2398.  
  2399. Directory  Tree  Overview
  2400.                              This chapter needs a quote. Suggestions, anyone?
  2401.  
  2402. This chapter describes the important parts of a standard Linux directory tree,
  2403. based on the FSSTND filesystem standard. It outlines the normal way of breaking
  2404. the di-rectory tree into separate filesystems with different purposes and gives
  2405. the motivation behind this particular split. Some alternative ways of splitting
  2406. are also described.
  2407.  
  2408.  
  2409.    META: The next version of the FSSTND (1.3?) will cause many minor changes,
  2410. and some new ones, due to work to make the FSSTND work for BSD systems as well.
  2411.  
  2412.  
  2413. 5.1   Background
  2414. This chapter is loosely based on the Linux filesystem standard, FSSTND, version
  2415. 1.2 (see the bibliography), which attempts to set a standard for how the
  2416. directory tree in a Linux system is organized. Such a standard has the advantage
  2417. that it will be easier to write or port software for Linux, and to administer
  2418. Linux machines, since everything will be in their usual places. There is no
  2419. authority behind the standard that forces anyone to comply to it, but it has got
  2420. the support of most, if not all Linux distributions. It is not a good idea to
  2421. break with the FSSTND without very compelling reasons.  The FSSTND attempts to
  2422. follow Unix tradition and current trends, making Linux systems familiar to those
  2423. with experience with other Unix systems, and vice versa.
  2424.  
  2425.    This chapter is not as detailed as the FSSTND. A system administrator should
  2426. also read the FSSTND for a complete understanding.
  2427.  
  2428.  
  2429.                                   53
  2430. 54                                     Chapter 5.  Directory Tree Overview
  2431.  
  2432.  
  2433.    This chapter does not explain all files in detail. The intention is not to
  2434. describe every file, but to give an overview of the system from a filesystem
  2435. point of view. Further information of each file is available elsewhere in this
  2436. manual or the manual pages.
  2437.  
  2438.    The full directory tree is intended to be breakable into smaller parts, each
  2439. on its own disk or partition, to accomodate to disk size limits and to ease
  2440. backup and other system administration. The major parts are the root, /usr,
  2441. /var, and /home filesystems. Each part has a different purpose. The directory
  2442. tree has been designed so that it works well in a network of Linux machines
  2443. which may share some parts of the filesystems over a read-only device (e.g., a
  2444. CD-ROM), or over the network with NFS.
  2445.  
  2446.    The roles of the different parts of the directory tree are described below. 
  2447.  
  2448.    oThe root filesystem is specific for each machine (it is generally stored on
  2449.    a local disk, although it could possibly be downloaded to a ramdisk during
  2450.    bootup) and contains the files that are necessary for booting the system up,
  2451.    and to bring it up to such a state that the other filesystems may be mounted.
  2452.    The contents of the root filesystem will therefore be sufficient for the
  2453.    single user state. It will also contain tools for fixing a broken system, and
  2454.    for recovering lost files from backups.
  2455.  
  2456.  
  2457.    oThe /usr filesystem contains all commands, libraries, manual pages, and
  2458.    other unchanging files needed during normal operation.  No files in /usr
  2459.    should be specific for any given machine, nor should they be modified during
  2460.    normal use. This allows the files to be shared over the network, which can be
  2461.    cost-effective since it saves disk space (there can easily be hundreds of
  2462.    megabytes in /usr), and can make administration easier (only the master /usr
  2463.    needs to be changed when updating an application, not each machine
  2464.    separately). Even if the filesystem is on a local disk, it could be mounted
  2465.    read-only, to lessen the chance of filesystem corruption during a crash.
  2466.  
  2467.  
  2468.    oThe /var filesystem contains files that change, such as spool directories
  2469.    (for mail, news, printers, etc), log files, formatted manual pages, and
  2470.    temporary files. Traditionally everything in /var has been somewhere below
  2471.    /usr, but that made it impossible to mount /usr read-only.
  2472.  
  2473.  
  2474.    oThe /home filesystem contains the users' home directories, i.e., all the
  2475.    real data on the system. Separating home directories to their own directory
  2476.    tree or filesystem makes backups easier; the other parts often do not have to
  2477.    be backed up, or at
  2478. 5.2.  The root filesystem                                                   55
  2479.  
  2480.  
  2481.  
  2482.    least not as often (they seldom change). A big /home might have to be broken
  2483.    on several filesystems, which requires adding an extra naming level below
  2484.    /home, e.g., /home/students and /home/staff.
  2485.  
  2486.  
  2487.  
  2488. Although the different parts have been called filesystems above, there is no
  2489. require- ment that they actually be on separate filesystems. They could easily
  2490. be kept in a single one if the system is a small single-user system and he wants
  2491. to keep things sim- ple. The directory tree might also be divided into
  2492. filesystems differently, depending on how large the disks are, and how space is
  2493. allocated for various purposes. The impor tant part, though, is that all the
  2494. standard names work; even if, say, /var and /usr are actually on the same
  2495. partition, the names /usr/lib/libc.a and /var/adm/messages must work, for
  2496. example by moving files below /var into /usr/var, and making /var a symlink to
  2497. /usr/var.
  2498.  
  2499.  
  2500.    The Unix filesystem structure groups files according to purpose, i.e., all
  2501. commands are in one place, all data files in another, documentation in a third,
  2502. and so on. An alternative would be to group files files according to the program
  2503. they belong to, i.e., all Emacs files would be in one directory, all TEX in
  2504. another, and so on.  The problem with the latter approach is that it makes it
  2505. difficult to share files (the program directory often contains both static and
  2506. shareable and changing and non-shareable files), and sometimes to even find the
  2507. files (e.g., manual pages in a huge number of places, and making the manual page
  2508. programs find all of them is a maintenance nightmare).
  2509.  
  2510. 5.2   The root filesystem
  2511. The root filesystem should generally be small, since it contains very critical
  2512. files and a small, infrequently modified filesystem has a better chance of not
  2513. getting corrupted. A corrupted root filesystem will generally mean that the
  2514. system becomes unbootable except with special measures (e.g., from a floppy), so
  2515. you don't want to risk it.
  2516.  
  2517.    The root directory generally doesn't contain any files, except perhaps the
  2518. standard boot image for the system, usually called /vmlinuz. All other files are
  2519. in subdirecto- ries in the root filesystems:
  2520.  
  2521. /bin           Commands needed during bootup that might be used by normal users
  2522.                (probably after bootup).
  2523.  
  2524. /sbin          Like /bin, but the commands are not intended for normal users,
  2525.                al-
  2526. 56                                     Chapter 5.  Directory Tree Overview
  2527.  
  2528.  
  2529.  
  2530.           though they may use them if necessary and allowed.
  2531.  
  2532.  
  2533. /etc           Configuration files specific to the machine.
  2534.  
  2535. /root          The home directory for user root.
  2536.  
  2537. /lib           Shared libraries needed by the programs on the root filesystem.
  2538.  
  2539. /lib/modules   Loadable kernel modules, especially those that are needed to boot
  2540.                the system when recovering from disasters (e.g., network and
  2541.                filesystem drivers).
  2542.  
  2543. /dev           Device files.
  2544.  
  2545. /tmp           Temporary files. Programs running after bootup should use
  2546.                /var/tmp, not /tmp, since the former is probably on a disk with
  2547.                more space.
  2548.  
  2549. /boot          Files used by the bootstrap loader, e.g., LILO. Kernel images are
  2550.                often kept here instead of in the root directory.  If there are
  2551.                many kernel images, the directory can easily grow rather big, and
  2552.                it might be better to keep it in a separate filesystem. Another
  2553.                reason would be to make sure the kernel images are within the
  2554.                first 1024 cylinders of an IDE disk.
  2555.  
  2556. /mnt           Mount point for temporary mounts by the system administrator.
  2557.                Pro-grams aren't supposed to mount on /mnt automatically. /mnt
  2558.                might be divided into subdirectories (e.g., /mnt/dosa might be
  2559.                the floppy drive using an MS-DOS filesystem, and /mnt/exta might
  2560.                be the same with an ext2 filesystem).
  2561.  
  2562.  
  2563. /proc,/usr,/var, /homeMount points for the other filesystems.
  2564.  
  2565. 5.2.1  The /etc directory
  2566.  
  2567.  
  2568.  
  2569. The /etc directory contains a lot of files. Some of them are described below.
  2570. For others, you should determine which program they belong to and read the
  2571. manual page for that program. Many networking configuration files are in /etc as
  2572. well, and are described in the Networking Administrators' Guide.
  2573.  
  2574. /etc/rc or /etc/rc.d or /etc/rc?.d
  2575.    Scripts or directories of scripts to run at startup or when changing the run
  2576.    level.  See the chapter on init for further
  2577. 5.2.  The root filesystem                                                   57
  2578.  
  2579.  
  2580.  
  2581.    information.
  2582.  
  2583.  
  2584. /etc/passwd
  2585.    The user database, with fields giving the username, real name, home
  2586.    directory, encrypted password, and other information about each user. The
  2587.    format is documented in the passwd(5) manual page.
  2588.  
  2589.  
  2590. /etc/fdprm
  2591.    Floppy disk parameter table. Describes what different floppy disk formats
  2592.    look like. Used by setfdprm(1). See the setfdprm(8) manual page for more
  2593.    information.
  2594.  
  2595.  
  2596. /etc/fstab
  2597.    Lists the filesystems mounted automatically at startup by the mount -a
  2598.    command (in /etc/rc or equivalent startup file). Under Linux, also contains
  2599.    information about swap areas used automatically by swapon -a. See section
  2600.    4.6.5 and the mount(8) manual page for more informa-tion.
  2601.  
  2602.  
  2603. /etc/group
  2604.    Similar to /etc/passwd, but describes groups instead of users. See the
  2605.    group(5) manual page for more information.
  2606.  
  2607.  
  2608. /etc/inittab
  2609.    Configuration file for init(8).
  2610.  
  2611.  
  2612. /etc/issue
  2613.    Output by getty before the login prompt.  Usually contains a short
  2614.    description or welcoming message to the system. The contents are up to the
  2615.    system administrator.
  2616.  
  2617.  
  2618. /etc/magic
  2619.    The configuration file for file(1). Contains the descriptions of various file
  2620.    formats based on which file guesses the type of the file. See the magic(8)
  2621.    and file(1) manual pages for more information.
  2622.  
  2623.  
  2624. /etc/motd
  2625.    The message of the day, automatically output after a successful login.
  2626.    Contents are up to the system administrator. Often used for getting
  2627.    information to every user, such as warnings about planned downtimes.
  2628.  
  2629.  
  2630. /etc/mtab
  2631.    List of currently mounted filesystems. Initially set up by the scripts, and
  2632.    updated automatically by the mount command. Used when a list of mounted
  2633.    filesystems is needed, e.g., by the df(1) command.
  2634.  
  2635.  
  2636. /etc/shadow
  2637.    Shadow password file on systems with shadow password software installed.
  2638.    Shadow passwords move the encrypted password from /etc/passwd into
  2639.    /etc/shadow; the latter is not readable by anyone except root. This makes it
  2640.    harder to crack passwords.
  2641. 58                                     Chapter 5.  Directory Tree Overview
  2642.  
  2643.  
  2644.  
  2645. /etc/login.defs
  2646.    Configuration file for the login(1) command.
  2647.  
  2648.  
  2649. /etc/printcap
  2650.    Like /etc/termcap, but intended for printers. Different syntax.
  2651.  
  2652.  
  2653. /etc/profile, /etc/csh.login, /etc/csh.cshrc
  2654.    Files executed at login or startup time by the Bourne or C shells. These
  2655.    allow the system administrator to set global defaults for all users. See the
  2656.    manual pages for the respective shells.
  2657.  
  2658.  
  2659. /etc/securetty
  2660.    Identifies secure terminals, i.e., the terminals from which root is allowed
  2661.    to log in. Typically only the virtual consoles are listed, so that it becomes
  2662.    impossible (or at least harder) to gain superuser privileges by breaking into
  2663.    a system over a modem or a network.
  2664.  
  2665.  
  2666. /etc/shells
  2667.    Lists trusted shells.  The chsh(1) command allows users to change their login
  2668.    shell only to shells listed in this file. ftpd, the server process that
  2669.    provides FTP services for a machine, will check that the user's shell is
  2670.    listed in /etc/shells and will not let people log in unles the shell is
  2671.    listed there.
  2672.  
  2673.  
  2674. /etc/termcap
  2675.    The terminal capability database.  Describes by what \escape sequences"
  2676.    various terminals can be controlled. Programs are written so that instead of
  2677.    directly outputting an escape sequence that only works on a particular brand
  2678.    of terminal, they look up the correct sequence to do whatever it is they want
  2679.    to do in /etc/termcap. As a result most programs work with most kinds of
  2680.    terminals.  See the termcap(5), curs_termcap(3), and terminfo(5) manual pages
  2681.    for more information.
  2682.  
  2683.  
  2684. META: HOSTNAME, adjtime, disktab, gettydefs, networking (exports, host.conf,
  2685. hosts, hosts.equiv, inetd.conf, named.*, networks, ntp.conf, protocols,
  2686. resolv.conf, rpc, services, syslog.conf), mtools, and so forth.
  2687.  
  2688. 5.2.2  The /dev directory
  2689.  
  2690.  
  2691.  
  2692. The /dev directory contains the special device files for all the devices. The
  2693. device files are named using special conventions; these are described in
  2694. appendix C. The device files are created during installation, and later with the
  2695. /dev/MAKEDEV script.  The /dev/MAKEDEV.local is a script written by the system
  2696. administrator that creates local-only device files or links (i.e., those that
  2697. are not part of the standard MAKEDEV,
  2698. 5.3.  The /usr filesystem                                                   59
  2699.  
  2700.  
  2701.  
  2702. such as device files for some non-standard device driver).
  2703.  
  2704.  
  2705.  
  2706. 5.3   The /usr filesystem
  2707.  
  2708. The /usr filesystem is often large, since all programs are installed there. All
  2709. files in /usr usually come from a Linux distribution; locally installed programs
  2710. and other stuff goes below /usr/local.  This makes it possible to update the
  2711. system from a new version of the distribution, or even a completely new
  2712. distribution, without having to install all programs again.  Some of the
  2713. subdirectories of /usr are listed below (some of the less important directories
  2714. have been dropped; see the FSSTND for more information).
  2715.  
  2716. /usr/X11R6
  2717.    The X Window System, all files.  To simplify the development and installation
  2718.    of X, the X files have not been integrated into the rest of the system. There
  2719.    is a directory tree below /usr/X11R6 similar to that below /usr itself.
  2720.  
  2721.  
  2722. /usr/X386
  2723.    Similar to /usr/X11R6, but for X11 Release 5.
  2724.  
  2725.  
  2726. /usr/bin
  2727.    Almost all user commands. Some commands are in /bin or in /usr/local/bin.
  2728.  
  2729.  
  2730. /usr/sbin
  2731.    System administration commands that are not needed on the root filesystem,
  2732.    e.g., most server programs.
  2733.  
  2734.  
  2735. /usr/man, /usr/info, /usr/doc
  2736.    Manual pages, GNU Info documents, and miscellaneous other documentation
  2737.    files, respectively.
  2738.  
  2739.  
  2740. /usr/include
  2741.    Header files for the C programming language. This should actually be below
  2742.    /usr/lib for consistency, but the tradition is overwhelmingly in support for
  2743.    this name.
  2744.  
  2745.  
  2746. /usr/lib
  2747.    Unchanging data files for programs and subsystems, including some site-wide
  2748.    configuration files. The name lib comes from library; originally libraries of
  2749.    programming subroutines were stored in /usr/lib.
  2750.  
  2751.  
  2752. /usr/local
  2753.    The place for locally installed software and other files.
  2754. 60                                     Chapter 5.  Directory Tree Overview
  2755.  
  2756.  
  2757.  
  2758. 5.4   The /var filesystem
  2759.  
  2760.  
  2761. The /var contains data that is changed when the system is running normally. It
  2762. isspecific for each system, i.e., not shared over the network with other
  2763. computers.
  2764.  
  2765. /var/catman
  2766.    A cache for man pages that are formatted on demand.  The source for manual
  2767.    pages is usually stored in /usr/man/man*; some manual pages might come with a
  2768.    pre-formatted version, which is stored in  /usr/man/cat*. Other manual pages
  2769.    need to be formatted when they are first viewed; the formatted version is
  2770.    then stored in /var/man so that the next person to view the same page won't
  2771.    have to wait for it to be formatted.  (/var/catman is often cleaned in the
  2772.    same way temporary directories are cleaned.)
  2773.  
  2774.  
  2775. /var/lib
  2776.    Files that change while the system is running normally.
  2777.  
  2778.  
  2779. /var/local
  2780.    Variable data for programs that are installed in /usr/local (i.e., programs
  2781.    that have been installed by the system administrator). Note that even locally
  2782.    installed programs should use the other /var directories if they are
  2783.    appropriate, e.g., /var/lock.
  2784.  
  2785.  
  2786. /var/lock
  2787.    Lock files. Many programs follow a convention to create a lock file in
  2788.    /var/lock to indicate that they are using a particular device or file. Other
  2789.    programs will notice the lock file and won't attempt to use the device or
  2790.    file.
  2791.  
  2792.  
  2793. /var/log
  2794.    Log files from various programs, especially login (/var/log/wtmp, which logs
  2795.    all logins ans logouts into the system) and syslog (/var/log/messages, where
  2796.    all kernel and system program message are usually stored). File in /var/log
  2797.    can often grow indefinitely, and may require cleaning at regular intervals.
  2798.  
  2799.  
  2800. /var/run
  2801.    Files that contain information about the system that is valid until the
  2802.    system is next booted. For example, /var/run/utmp contains information about
  2803.    people currently logged in.
  2804.  
  2805.  
  2806. /var/spool
  2807.    Directories for mail, news, printer queues, and other queued work. Each
  2808.    different spool has its own subdirectory below /var/spool, e.g., the
  2809.    mailboxes of the users are in /var/spool/mail.
  2810. 5.5.  The /proc filesystem                                                  61
  2811.  
  2812.  
  2813.  
  2814. /var/tmp
  2815.    Temporary files that are large or that need to exist for a longer time than
  2816.    what is allowed for /tmp.  (Although the system administrator might not allow
  2817.    very old files in /var/tmp either.)
  2818.  
  2819. 5.5   The /proc filesystem
  2820.  
  2821. The /proc filesystem contains a illusionary filesystem. It does not exist on a
  2822. disk. Instead, the kernel creates it in memory. It is used to provide
  2823. information about the system (originally about processes, hence the name).  Some
  2824. of the more important files and directories are explained below. The /proc
  2825. filesystem is described in more detail in the proc(5) manual page.
  2826.  
  2827.  
  2828.  
  2829. /proc/1
  2830.    A directory with information about process number 1. Each process has a
  2831.    directory below /proc with the name being its process identification number.
  2832.  
  2833.  
  2834. /proc/cpuinfo
  2835.    Information about the processor, such as its type, make, model, and
  2836.    perfomance.
  2837.  
  2838.  
  2839. /proc/devices
  2840.    List of device drivers configured into the currently running kernel.
  2841.  
  2842.  
  2843. /proc/dma
  2844.    Shows which DMA channels are being used at the moment.
  2845.  
  2846.  
  2847. /proc/filesystems
  2848.    Filesystems configured into the kernel.
  2849.  
  2850.  
  2851. /proc/interrupts
  2852.    Shows which interrupts are in use, and how many of each there have been.
  2853.  
  2854.  
  2855. /proc/ioports
  2856.    Which I/O ports are in use at the moment.
  2857.  
  2858.  
  2859. /proc/kcore
  2860.    An image of the physical memory of the system. This is exactly the same size
  2861.    as your physical memory, but does not really take up that much memory; it is
  2862.    generated on the fly as programs access it. (Remember: unless you copy it
  2863.    elsewhere, nothing under /proc takes upany disk space at all.)
  2864.  
  2865.  
  2866. /proc/kmsg
  2867.    Messages output by the kernel. These are also routed to syslog.
  2868.  
  2869.  
  2870. /proc/ksyms
  2871.    Symbol table for the kernel.
  2872. 62                                     Chapter 5.  Directory Tree Overview
  2873.  
  2874.  
  2875.  
  2876. /proc/loadavg
  2877.    The `load average' of the system; three meaningless indicators of how much
  2878.    work the system has to do at the moment.
  2879.  
  2880.  
  2881. /proc/meminfo
  2882.    Information about memory usage, both physical and swap.
  2883.  
  2884.  
  2885. /proc/modules
  2886.    Which kernel modules are loaded at the moment.
  2887.  
  2888.  
  2889. /proc/net
  2890.    Status information about network protocols.
  2891.  
  2892.  
  2893. /proc/self
  2894.    A symbolic link to the process directory of the program that is looking at
  2895.    /proc. When two processes look at /proc, they get different links. This is
  2896.    mainly a convenience to make it easier for programs to get at their process
  2897.    directory.
  2898.  
  2899.  
  2900. /proc/stat
  2901.    Various statistics about the system, such as the number of page faults since
  2902.    the system was booted.
  2903.  
  2904.  
  2905. /proc/uptime
  2906.    The time the system has been up.
  2907.  
  2908.  
  2909. /proc/version
  2910.    The kernel version.
  2911.  
  2912.  
  2913.  
  2914. Note that while the above files tend to be easily readable text files, they can
  2915. sometimes be formatted in a way that is not easily digestable. There are many
  2916. commands that do little more than read the above files and format them for
  2917. easier understanding. For example, the free program reads /proc/meminfo and
  2918. converts the amounts given in bytes to kilobytes (and adds a little more
  2919. information, as well).
  2920.  
  2921.  
  2922. Chapter  6
  2923.  
  2924.  
  2925.  
  2926. Memory  Management
  2927.  
  2928.  
  2929.  
  2930.       Minnet, jag har tappat mitt minne,
  2931.                "ar jag svensk eller finne
  2932.                   kommer inte ihag: : :
  2933.  
  2934.               Inne, "ar jag ute eller inne
  2935.                jag har luckor i minnet,
  2936.                 sad"ar sma ALKO-HAL
  2937.                         Men besinne,
  2938.     man t"atar med det br"annvin man far,
  2939.            fast"an minnet och helan gar.
  2940.  
  2941.                                          (Bosse "Osterberg)
  2942.  
  2943. This section describes the Linux memory management features, i.e., virtual
  2944. memory and the disk buffer cache.  The purpose and workings and the things the
  2945. system administrator needs to take into consideration are described.
  2946.  
  2947. 6.1   What is virtual memory?
  2948.  
  2949.  
  2950.  
  2951. Linux supports virtual memory, that is, using a disk as an extension of RAM so
  2952. that the effective size of usable memory grows correspondingly. The kernel will
  2953. write the contents of a currently unused block of memory to the hard disk so
  2954. that the memory can be used for another purpose.  When the original contents are
  2955. needed again, they are read back into memory.  This is all made completely
  2956. transparent
  2957.  
  2958.  
  2959.                                   63
  2960. 64                                         Chapter 6.  Memory Management
  2961.  
  2962.  
  2963.  
  2964. to the user; programs running under Linux only see the larger amount of memory
  2965. available and don't notice that parts of them reside on the disk from time to
  2966. time. Of course, reading and writing the hard disk is slower (on the order of a
  2967. thousand times slower) than using real memory, so the programs don't run as
  2968. fast. The part of the hard disk that is used as virtual memory is called the
  2969. swap space.
  2970.  
  2971.  
  2972.    Linux can use either a normal file in the filesystem or a separate partition
  2973. for swap space.  A swap partition is faster, but it is easier to change the size
  2974. of a swap file (there's no need to repartition the whole hard disk, and possibly
  2975. install everything from scratch). When you know how much swap space you need,
  2976. you should go for a swap partition, but if you are uncertain, you can use a swap
  2977. file first, use the system for a while so that you can get a feel for how much
  2978. swap you need, and then make a swap partition when you're confident about its
  2979. size.
  2980.  
  2981.  
  2982.    You should also know that Linux allows one to use several swap partitions
  2983. and/or swap files at the same time. This means that if you only occasionally
  2984. need an unusual amount of swap space, you can set up an extra swap file at such
  2985. times, instead of keeping the whole amount allocated all the time.
  2986.  
  2987. 6.2   Creating a swap area
  2988.  
  2989.  
  2990.  
  2991. A swap file is an ordinary file; it is in no way special to the kernel. The only
  2992. thing that matters to the kernel is that it has no holes, and that it is
  2993. prepared for use with mkswap(8).  It must reside on a local disk, however; it
  2994. can't reside in a filesystem that has been mounted over NFS.
  2995.  
  2996.  
  2997.    The bit about holes is important. The swap file reserves the disk space so
  2998. that the kernel can quickly swap out a page without having to go through all the
  2999. things that are necessary when allocating a disk sector to a file. The kernel
  3000. merely uses any sectors that have already been allocated to the file. Because a
  3001. hole in a file means that there are no disk sectors allocated (for that place in
  3002. the file), it is not good for the kernel to try to use them.
  3003.  
  3004.  
  3005.    One good way to create the swap file without holes is through the following
  3006. com- mand:
  3007.  
  3008.  
  3009.  
  3010.     ttyp5 root " $ dd if=/dev/zero of=/extra-swap bs=1024 count=1024
  3011.  
  3012.     1024+0 records in
  3013.  
  3014.     1024+0 records out
  3015.  
  3016.     ttyp5 root " $
  3017. 6.3.  Using a swap area                                                    65
  3018.  
  3019.  
  3020.  
  3021. where /extra-swap is the name of the swap file and the size of is given after
  3022. the count=. It is best for the size to be a multiple of 4, because the kernel
  3023. writes out memory pages, which are 4 kilobytes in size. If the size is not a
  3024. multiple of 4, the last couple of kilobytes may be unused.
  3025.  
  3026.  
  3027.    A swap partition is also not special in any way. You create it just like any
  3028. other partition; the only difference is that it is used as a raw partition, that
  3029. is, it will not contain any filesystem at all. It is a good idea to mark swap
  3030. partitions as type 82 (Linux swap); this will the make partition listings
  3031. clearer, even though it is not strictly necessary to the kernel.
  3032.  
  3033.  
  3034.    After you have created a swap file or a swap partition, you need to write a
  3035. signature to its beginning; this contains some administrative information and is
  3036. used by the kernel. The command to do this is mkswap(8), used like this:
  3037.  
  3038.  
  3039.  
  3040.     ttyp5 root " $ mkswap /extra-swap 1024
  3041.  
  3042.     Setting up swapspace, size = 1044480 bytes
  3043.  
  3044.     ttyp5 root " $
  3045.  
  3046.  
  3047.  
  3048. Note that the swap space is still not in use yet: it exists, but the kernel does
  3049. not use it to provide virtual memory.
  3050.  
  3051.  
  3052.    The Linux memory manager limits the size of each swap area to 127.5 MB. A
  3053. larger swap space can be created, but only the first 127.5 MB are actually used.
  3054. You can, however, use up to 16 swap spaces simultaneously, for a total of almost
  3055. 2 GB.1
  3056.  
  3057. 6.3   Using a swap area
  3058.  
  3059.  
  3060.  
  3061. An initialized swap area is taken into use with swapon(8). This command tells
  3062. the kernel that the swap area can be used.  The path to the swap area is given
  3063. as the argument, so to start swapping on a temporary swap file one might use the
  3064. following command.
  3065.  
  3066.     swapon /usr/tmp/temporary-swap-file ttyp5 root " $ swapon /extra-swap
  3067.     ttyp5 root " $
  3068.  
  3069. Swap areas can be used automatically by listing them in the /etc/fstab file.
  3070.  
  3071.  
  3072.     /dev/hda8 swap swap defaults
  3073. _____________________________1
  3074.    A gigabyte here, a gigabyte there, pretty soon we start talking about real
  3075. memory.
  3076. 66                                         Chapter 6.  Memory Management
  3077.  
  3078.  
  3079.  
  3080. The startup scripts will run the command swapon -a, which will start swapping on
  3081. all the swap areas listed in /etc/fstab. Therefore, the swapon command is
  3082. usually used only when extra swap is needed.
  3083.  
  3084.  
  3085.  
  3086.    You can monitor the use of swap areas with free(1). It will tell the total
  3087. amount of swap space used. The same information is available via top(1), or
  3088. using the proc filesystem in file /proc/meminfo. It is currently difficult to
  3089. get information on the use of a specific swap area.
  3090.  
  3091.  
  3092.  
  3093.    A swap area can be removed from use with swapoff(8). It is usually not
  3094. necessary to do it, except for temporary swap areas.  Any pages in use in the
  3095. swap area are swapped in first; if there is not sufficient physical memory to
  3096. hold them, they will then be swapped out (to some other swap area). If there is
  3097. not enough virtual memory to hold all of the pages Linux will start to trash;
  3098. after a long while it should recover, but meanwhile the system is unusable. You
  3099. should check (e.g., with free) that there is enough free memory before removing
  3100. a swap space from use.
  3101.  
  3102.  
  3103.  
  3104.    All the swap areas that are used automatically with swapon -a can be removed
  3105. from use with swapoff -a; it looks at the file /etc/fstab to find what to
  3106. remove. Any manually used swap areas will remain in use.
  3107.  
  3108.  
  3109.  
  3110.    Sometimes a lot of swap space can be in use even though there is a lot of
  3111. free physical memory. This can happen for instance if at one point there is need
  3112. to swap, but later a big process that occupied much of the physical memory
  3113. terminates and frees the memory. The swapped-out data is not automatically
  3114. swapped in until it is needed, so the physical memory may remain free for a long
  3115. time. There is no need to worry about this, but it can be comforting to know
  3116. what is happening.
  3117.  
  3118.  
  3119.  
  3120. 6.4   Sharing swap areas with other operating systems
  3121.  
  3122. Virtual memory is built into many operating systems. Since they each need it
  3123. only when they are running, i.e., never at the same time, the swap areas of all
  3124. but the currently running one are being wasted. It would be more efficient for
  3125. them to share a single swap area. This is possible, but can require a bit of
  3126. hacking. The Tips-HOWTO contains some advice on how to implement this.
  3127. 6.5.  Allocating swap space                                                  67
  3128.  
  3129.  
  3130.  
  3131. 6.5   Allocating swap space
  3132.  
  3133.  
  3134.  
  3135. Some people will tell you that you should allocate twice as much swap space as
  3136. you have physical memory, but this is a bogus rule. Here's how to do it
  3137. properly:
  3138.  
  3139.  
  3140.   1.Estimate your total memory needs. This is the largest amount of memory
  3141. you'll probably need at a time, that is the sum of the memory requirements of
  3142. all the programs you want to run at the same time. This can be done by running
  3143. at the same time all the programs you are likely to ever be running at the same
  3144. time. For instance, if you want to run X, you should allocate about 8 MB for it,
  3145. gcc wants several megabytes (some files need an unusually large amount, up to
  3146. several tens of megabytes, but usually about four should do), and so on. The
  3147. kernel will use about a megabyte by itself, and the usual shells and other small
  3148. utilities perhaps a few hundred kilobytes (say a megabyte together). There is no
  3149. need to try to be exact, rough estimates are fine, but you might want to be on
  3150. the pessimistic side. Remember that if there are going to be several people
  3151. using the system at the same time, they are all going to consume memory.
  3152. (However, if two people run the same program at the same time, the total memory
  3153. consumption is usually not double, since code pages and shared libraries exist
  3154. only once.) The free(8) and ps(1) commands are useful for estimating the memory
  3155. needs.
  3156.  
  3157.  
  3158.   2.Add some security to the estimate in step 1. This is because estimates of
  3159. program sizes will probably be wrong, because you'll probably forget some
  3160. programs you want to run, and to make certain that you have some extra space
  3161. just in case. A couple of megabytes should be fine. (It is better to allocate
  3162. too much than too little swap space, but there's no need to over-do it and
  3163. allocate the whole disk, since unused swap space is wasted space; see later
  3164. about adding more swap.) Also, since it is nicer to deal with even numbers, you
  3165. can round the value up to the next full megabyte.
  3166.  
  3167.  
  3168.   3.Based on the computations in steps 1 and 2, you know how much memory you'll
  3169. be needing in total. So, in order to allocate swap space, you just need to
  3170. subtract the size of your physical memory from the total memory needed, and you
  3171. know how much swap space you need. (On some versions of UNIX, you need to
  3172. allocate space for an image of the physical memory as well, so the amount
  3173. computed in step 2 is what you need and you shouldn't do the subtraction.)
  3174.  
  3175.  
  3176.   4.If your calculated swap area is very much larger than your physical memory
  3177. (more than a couple times larger), you should probably invest in more physical
  3178. memory, otherwise performance will be too low.
  3179. 68                                         Chapter 6.  Memory Management
  3180.  
  3181.  
  3182.  
  3183. 6.6   The buffer cache
  3184.  
  3185.  
  3186.  
  3187. Reading from a disk2 is very slow compared to accessing (real) memory. In
  3188. addition, it is common to read the same part of a disk several times during
  3189. relatively short periods of time. For example, one might first read an e-mail
  3190. message, then read the letter into an editor when replying to it, then make the
  3191. mail program read it again when copying it to a folder. Or, consider how often
  3192. the command ls might be run on a system with many users. By reading the
  3193. information from disk only once and then keeping it in memory until no longer
  3194. needed, one can speed up all but the first read. This is called disk buffering,
  3195. and the memory used for the purpose is called the buffer cache.
  3196.  
  3197.  
  3198.    Since memory is, unfortunately, a finite, nay, scarce resource, the buffer
  3199. cache usually cannot be big enough (it can't hold all the data one ever wants to
  3200. use). When the cache fills up, the data that has been unused for the longest
  3201. time is discarded and the memory thus freed is used for the new data.
  3202.  
  3203.  
  3204.    Disk buffering works for writes as well.  On the one hand, data that is
  3205. written is often soon read again (e.g., a source code file is saved to a file,
  3206. then read by the compiler), so putting data that is written in the cache is a
  3207. good idea. On the other hand, by only putting the data into the cache, not
  3208. writing it to disk at once, the program that writes runs quicker. The writes can
  3209. then be done in the background, without slowing down the other programs.
  3210.  
  3211.  
  3212.    Most operating systems have buffer caches (although they might be called
  3213. some- thing else), but not all of them work according to the above principles. 
  3214. Some are write-through: the data is written to disk at once (it is kept in the
  3215. cache as well, of course).  The cache is called write-back if the writes are
  3216. done at a later time. Write-back is more efficient than write-through, but also
  3217. a bit more prone to errors: if the machine crashes, or the power is cut at a bad
  3218. moment, or the floppy is removed from the disk drive before the data in the
  3219. cache waiting to be written gets written, the changes in the cache are usually
  3220. lost. This might even mean that the filesystem (if there is one) is not in full
  3221. working order, perhaps because the unwritten data held important changes to the
  3222. bookkeeping information. Because of this, you should never turn off the power
  3223. without using a proper shutdown procedure (see an as yet unwrit- ten chapter),
  3224. or remove a floppy from the disk drive until it has been unmounted (if it was
  3225. mounted) or after whatever program is using it has signaled that it is finished
  3226. and the floppy drive light doesn't shine anymore.  The sync(8) command flushes
  3227. the buffer, i.e., forces all unwritten data to be written to disk, and can be
  3228. used when
  3229. _____________________________2
  3230.    Except a RAM disk, for obvious reasons.
  3231. 6.6.  The buffer cache                                                     69
  3232.  
  3233.  
  3234.  
  3235. one wants to be sure that everything is safely written. In traditional UNIX
  3236. systems, there is a program running in the background which does a sync every 30
  3237. seconds, so it is usually not necessary to use sync. Linux has an additional
  3238. daemon, bdflush(8), that does a more imperfect sync more frequently to avoid the
  3239. sudden freeze due to heavy disk I/O that sync sometimes causes.
  3240.  
  3241.  
  3242.    The cache does not actually buffer files, but blocks, which are the smallest
  3243. units of disk I/O (under Linux, they are usually 1 kB). This way, also
  3244. directories, super blocks, other filesystem bookkeeping data, and non-filesystem
  3245. disks are cached.
  3246.  
  3247.  
  3248.    The effectiveness of a cache is primarily decided by its size. A small cache
  3249. is next to useless: it will hold so little data that all all cached data is
  3250. flushed from the cache before it is reused. The critical size depends on how
  3251. much data is read and written, and how often the same data is accessed. The only
  3252. way to know is to experiment.
  3253.  
  3254.  
  3255.    If the cache is of a fixed size, it is not very good to have it too big,
  3256. either, because that might make the free memory too small and cause swapping
  3257. (which is also slow). To make the most efficient use of real memory, Linux
  3258. automatically uses all free RAM for buffer cache, but also automatically makes
  3259. the cache smaller when programs need more memory.
  3260.  
  3261.  
  3262.    Under Linux, you do not need to do anything to make use of the cache, it
  3263. happens completely automatically. Except for following the proper procedures for
  3264. shutdown and removing floppies, you do not need to worry about it.
  3265. 70                                         Chapter 6.  Memory Management
  3266.  
  3267.  
  3268.  
  3269.  
  3270.  
  3271. Chapter  7
  3272.  
  3273.  
  3274.  
  3275. Logging  In  And  Out
  3276.                              This chapter needs a quote. Suggestions, anyone?
  3277.  
  3278. This section describes what happens when a user logs in or out. The various
  3279. inter- actions of background processes, log files, configuration files, and so
  3280. on are described in some detail. 
  3281.  
  3282. 7.1   Logins via terminals
  3283.  
  3284. Figure 7.1 shows how logins happen via terminals. First, init makes sure there
  3285. is a getty program for the terminal connection (or console). getty listens at
  3286. the terminal and waits for the user to notify that he is ready to login in (this
  3287. usually means that the user must type something).  When it notices a user, getty
  3288. outputs a welcome message (stored in /etc/issue), and prompts for the username,
  3289. and finally runs the login program. login gets the username as a parameter, and
  3290. prompts the user for the password. If these match, login starts the shell
  3291. configured for the user; else it just exits and terminates the process (perhaps
  3292. after giving the user another chance at entering the username and password).
  3293. init notices that the process terminated, and starts a new getty for the
  3294. terminal. 
  3295.  
  3296.    Note that the only process new process is created by init (using the fork(2)
  3297. system call); getty and login only replace the program running in the process
  3298. (using the exec(3) system call). 
  3299.  
  3300.    A separate program for noticing the user is needed for serial lines, since it
  3301. can be (and traditionally was) complicated to notice when a terminal becomes
  3302. active. getty 
  3303.  
  3304.                                   71
  3305.  
  3306. 72                                          Chapter 7.  Logging In And Out
  3307.  
  3308.   also adapts to the speed and other settings of the connection, which is
  3309. important especially for dial-in connections, where these parameters may change
  3310. from call to call. 
  3311.  
  3312.    There are several versions of getty and init in use, all with their good and
  3313. bad points. It is a good idea to learn about the versions on your system, and
  3314. also about the other versions (you could use the Linux Software Map to search
  3315. them). If you don't have dial-in's, you probably don't have to worry about
  3316. getty, but init is still important. 7.2   Logins via the network  Two computers
  3317. in the same network are usually linked via a single physical cable. When they
  3318. communicate over the network, the programs in each computer that take part in
  3319. the communication are linked via a virtual connection, a sort of imaginary
  3320. cable. As far as the programs at either end of the virtual connection are
  3321. concerned, they have a monopoly on their own cable. However, since the cable is
  3322. not real, only imaginary, the operating systems of both computers can have
  3323. several virtual con- nections share the same physical cable. This way, using
  3324. just a single cable, several programs can communicate without having to know of
  3325. or care about the other com- munications. It is even possible to have several
  3326. computers use the same cable; the virtual connections exist between two
  3327. computers, and the other computers ignore those connections that they don't take
  3328. part in. 
  3329.  
  3330.    That's a complicated and over-abstracted description of the reality.  It
  3331. might, however, be good enough to understand the important reason why network
  3332. logins are somewhat different from normal logins. The virtual connections are
  3333. established when there are two programs on different computers that wish to
  3334. communicate. Since it is in principle possible to login from any computer in a
  3335. network to any other computer, there is a huge number of potential virtual
  3336. communications. Because of this, it is not practical to start a getty for each
  3337. potential login. 
  3338.  
  3339.    There is a single process corresponding to getty that handles all network
  3340. logins. When it notices an incoming network login (i.e., it notices that it gets
  3341. a new virtual connection to some other computer), it starts a new process to
  3342. handle that single login. The original process remains and continues to listen
  3343. for new logins. 
  3344.  
  3345.    To make things a bit more complicated, there is more than one communication
  3346. protocol for network logins. The two most important ones are telnet and rlogin.
  3347. In addition to logins, there are many other virtual connections that may be made
  3348. (for
  3349.  
  3350. 7.3.  What login does                                                     73
  3351.  
  3352.   FTP, Gopher, HTTP, and other network services). It would be ineffective to
  3353. have a separate process listening for a particular type of connection, so
  3354. instead there is only one listener that can recognize the type of the connection
  3355. and can start the correct type of program to provide the service. This single
  3356. listener is called inetd; see the \Linux Network Administrators' Guide" for more
  3357. information.
  3358.  
  3359. 7.3   What login does
  3360.  
  3361. The login program takes care of authenticating the user (making sure that the
  3362. user- name and password match), and of setting up an initial environment for the
  3363. user by setting permissions for the serial line and starting the shell. 
  3364.  
  3365.    Part of the initial setup is outputting the contents of the file /etc/motd
  3366. (short for message of the day) and checking for electronic mail. These can be
  3367. disabled by creating a file called .hushlogin in the user's home directory. 
  3368.  
  3369.    If the file /etc/nologin exists, logins are disabled. That file is typically
  3370. created by shutdown(8) and relatives. login checks for this file, and will
  3371. refuse to accept a login if it exists. If it does exist, login outputs it
  3372. contents to the terminal before it quits. 
  3373.  
  3374.    login logs all failed login attempts in a system log file (via syslog). It
  3375. also logs all logins by root. Both of these can be useful when tracking down
  3376. intruders. 
  3377.  
  3378.    Currently logged in people are listed in /var/run/utmp. This file is valid
  3379. only until the system is next rebooted or shut down; it is cleared when the
  3380. system is booted. It lists each user and the terminal (or network connection) he
  3381. is using, along with some other useful information. The who, w, and other
  3382. similar commands look in utmp to see who are logged in. 
  3383.  
  3384.    All successful logins are recorded into /var/log/wtmp. This file will grow
  3385. without limit, so it must be cleaned regularly, for example by having a weekly
  3386. cron job to clear it.1 The last command browses wtmp. 
  3387.  
  3388.    Both utmp and wtmp are in a binary format (see the utmp(5) manual page); it
  3389. is unfortunately not convenient to examine them without special programs. 
  3390.  
  3391. _____________________________1
  3392.  
  3393.    Good Linux distributions do this out of the box.
  3394.  
  3395. 74                                          Chapter 7.  Logging In And Out  7.4
  3396.  
  3397.    X and xdm
  3398.  
  3399. META: X implements logins via xdm; also: xterm -ls
  3400.  
  3401. 7.5   Access control
  3402.  
  3403. The user database is traditionally contained in the /etc/passwd file. Some
  3404. systems use shadow passwords, and have moved the passwords to /etc/shadow. Sites
  3405. with many computers that share the accounts use NIS or some other method to
  3406. store the user database; they might also automatically copy the database from
  3407. one central location to all other computers. 
  3408.  
  3409.    The user database contains not only the passwords, but also some additional
  3410. infor- mation about the users, such as their real names, home directories, and
  3411. login shells. This other information needs to be public, so that anyone can read
  3412. it. Therefore the password is stored encrypted. This does have the drawback that
  3413. anyone with access to the encrypted password can use various cryptographical
  3414. methods to guess it, with- out trying to actually log into the computer.  Shadow
  3415. passwords try to avoid this by moving the password into another file, which only
  3416. root can read (the password is still stored encrypted). However, installing
  3417. shadow passwords later onto a system that did not support them can be difficult.
  3418.  
  3419.  
  3420.    With or without passwords, it is important to make sure that all passwords in
  3421. a system are good, i.e., not easily guessable. The crack program can be used to
  3422. crack passwords; any password it can find is by definition not a good one. 
  3423. While crack can be run be intruders, it can also be run by the system
  3424. adminstrator to avoid bad passwords. Good passwords can also be enforced by the
  3425. passwd(1) program; this is in fact more effective in CPU cycles, since cracking
  3426. passwords requires quite a lot of computation. 
  3427.  
  3428.    The user group database is kept in /etc/group; for systems with shadow pass-
  3429. words, there can be a /etc/shadow.group. 
  3430.  
  3431.    root usually can't login via most terminals or the network, only via
  3432. terminals listed in the /etc/securetty file. This makes it necessary to get
  3433. physical access to one of these terminals. It is, however, possible to log in
  3434. via any terminal as any other user, and use the su command to become root.
  3435.  
  3436. 7.6.  Shell startup                                                        75
  3437.  
  3438.   7.6   Shell startup  When an interactive login shell starts, it automatically
  3439. executes one or more pre- defined files.  Different shells execute different
  3440. files; see the documentation of each shell for further information. 
  3441.  
  3442.    Most shells first run some global file, for example, the Bourne shell
  3443. (/bin/sh) and its derivatives execute /etc/profile; in addition, they execute
  3444. "/.profile. /etc/profile allows the system administrator to have set up a common
  3445. user en- vironment, especially by setting the PATH to include local command
  3446. directories in addition to the normal ones. On the other hand, "/.profile allows
  3447. the user to cus- tomize the environment to his own tastes by overriding, if
  3448. necessary, the default environment.
  3449.  
  3450. 76                                          Chapter 7.  Logging In And Out   
  3451.  
  3452. Figure 7.1:  Logins via terminals:  the interaction of init, getty, login, and
  3453. the shell (here,
  3454.  
  3455. /bin/sh).
  3456.  
  3457.  Appendix  A
  3458.  
  3459.  
  3460.  
  3461. Design  and  Implementation  of  the  Second  Extended  Filesystem  This
  3462. appendix is a paper written by R'emy Card (card@masi.ibp.fr), Theodore Ts'o
  3463. (tytso@mit.edu), and Stephen Tweedie (sct@dcs.ed.ac.uk), the designers and
  3464. implementors of the ext2 filesystem. It was first published in the Proceedings
  3465. of the First Dutch International Symposium on Linux, ISBN 90 367 0385 9.
  3466. Introduction  Linux is a Unix-like operating system, which runs on PC-386
  3467. computers.  It was implemented first as extension to the Minix operating system
  3468. [9] and its first versions included support for the Minix filesystem only.  The
  3469. Minix filesystem contains two serious limitations: block addresses are stored in
  3470. 16 bit integers, thus the maximal filesystem size is restricted to 64 mega
  3471. bytes, and directories contain fixed-size entries and the maximal file name is
  3472. 14 characters. 
  3473.  
  3474.    We have designed and implemented two new filesystems that are included in the
  3475. standard Linux kernel. These filesystems, called \Extended File System" (Ext fs)
  3476. and \Second Extended File System" (Ext2 fs) raise the limitations and add new
  3477. features. 
  3478.  
  3479.    In this paper, we describe the history of Linux filesystems. We briefly
  3480. introduce the fundamental concepts implemented in Unix filesystems. We present
  3481. the implemen- tation of the Virtual File System layer in Linux and we detail the
  3482. Second Extended File System kernel code and user mode tools. Last, we present
  3483. performance measure- ments made on Linux and BSD filesystems and we conclude
  3484. with the current status
  3485.  
  3486.                                   77
  3487.  
  3488. 78
  3489.  
  3490.      Appendix A. Design and Implementation of the Second Extended Filesystem  of
  3491. Ext2fs and the future directions. A.1    History of Linux filesystems  In its
  3492. very early days, Linux was cross-developed under the Minix operating system. It
  3493. was easier to share disks between the two systems than to design a new
  3494. filesystem, so Linus Torvalds decided to implement support for the Minix
  3495. filesystem in Linux. The Minix filesystem was an efficient and relatively
  3496. bug-free piece of software. 
  3497.  
  3498.    However, the restrictions in the design of the Minix filesystem were too
  3499. limiting, so people started thinking and working on the implementation of new
  3500. filesystems in Linux. 
  3501.  
  3502.    In order to ease the addition of new filesystems into the Linux kernel, a
  3503. Virtual File System (VFS) layer was developed. The VFS layer was initially
  3504. written by Chris Provenzano, and later rewritten by Linus Torvalds before it was
  3505. integrated into the Linux kernel. It will be described in section A.3 of this
  3506. paper. 
  3507.  
  3508.    After the integration of the VFS in the kernel, a new filesystem, called the
  3509. \Ex- tended File System" was implemented in April 1992 and added to Linux 0.96c.
  3510. This new filesystem removed the two big Minix limitations: its maximal size was
  3511. 2 giga bytes and the maximal file name size was 255 characters. It was an
  3512. improvement over the Minix filesystem but some problems were still present in
  3513. it. There was no support for the separate access, inode modification, and data
  3514. modification timestamps. The filesystem used linked lists to keep track of free
  3515. blocks and inodes and this produced bad performances:  as the filesystem was
  3516. used, the lists became unsorted and the filesystem became fragmented. 
  3517.  
  3518.    As a response to these problems, two new filesytems were released in Alpha
  3519. version in January 1993: the Xia filesystem and the Second Extended File System.
  3520. The Xia filesystem was heavily based on the Minix filesystem kernel code and
  3521. only added a few improvements over this filesystem. Basically, it provided long
  3522. file names, support for bigger partitions and support for the three timestamps.
  3523. On the other hand, Ext2fs was based on the Extfs code with many reorganizations
  3524. and many improvements. It had been designed with evolution in mind and contained
  3525. space for future improvements. It will be described with more details in section
  3526. A.4. 
  3527.  
  3528.    When the two new filesystems were first released, they provided essentially
  3529. the same features. Due to its minimal design, Xia fs was more stable than
  3530. Ext2fs. As the filesystems were used more widely, bugs were fixed in Ext2fs and
  3531. lots of improvements
  3532.  
  3533. A.2.  Basic File System Concepts                                             79
  3534.  
  3535.   and new features were integrated. Ext2fs is now very stable and has become the
  3536. de- facto standard Linux filesystem. 
  3537.  
  3538.    The table A.1 contains a summary of the features provided by the different
  3539. filesys- tems.  
  3540.  
  3541.                    Table A.1: Summary of the filesystem features
  3542.            ____________________________________________________
  3543.            |                |Minix FS|Ext FS |Ext2 FS |Xia FS |
  3544.            |________________|________|_______|________|_______|
  3545.            | Max FS size    |64 MB   | 2 GB  | 4 TB   | 2 GB  |
  3546.            |                |        |       |        |       |
  3547.            | Max file size  |64|MB   |  2 GB | 2 GB   | 64 MB |
  3548.            |                |        |       |        |       |
  3549.            | Max file name  |16/30 c |255 c  |255 c   |248 c  |
  3550.            |                |        |       |        |       |
  3551.            | 3 times support|No      |  No   |  Yes   | Yes   |
  3552.            |                |        |       |        |       |
  3553.            | Extensible     |No      |  No   |  Yes   | No    |
  3554.            |                |        |       |        |       |
  3555.            | Var. block size|No      | No    |  Yes   | No    |
  3556.            |                |        |       |        |       |
  3557.            | Maintained     | Yes    | No    |  Yes   |  ?    |
  3558.            |________________|________|_______|________|_______|
  3559.  
  3560.  
  3561.  
  3562. A.2    Basic File System Concepts
  3563.  
  3564.  
  3565.  
  3566. Every Linux filesystem implements a basic set of common concepts derivated from
  3567. the Unix operating system [2]: files are represented by inodes, directories are
  3568. simply files containing a list of entries and devices can be accessed by
  3569. requesting I/O on special files.
  3570.  
  3571. A.2.1  Inodes
  3572.  
  3573.   Each file is represented by a structure, called an inode.  Each inode contains
  3574. the description of the file: file type, access rights, owners, timestamps, size,
  3575. pointers to data blocks. The addresses of data blocks allocated to a file are
  3576. stored in its inode. When a user requests an I/O operation on the file, the
  3577. kernel code converts the current offset to a block number, uses this number as
  3578. an index in the block addresses table and reads or writes the physical block.
  3579. Figure A.1 represents the structure of an inode.
  3580.  
  3581. 80     Appendix A. Design and Implementation of the Second Extended Filesystem
  3582.  
  3583.                           Figure A.1: Structure of an inode  
  3584. A.2.2  Directories
  3585.  
  3586.   Directories are structured in a hierarchical tree. Each directory can contain
  3587. files and subdirectories. 
  3588.  
  3589.    Directories are implemented as a special type of files. Actually, a directory
  3590. is a file containing a list of entries. Each entry contains an inode number and
  3591. a file name. When a process uses a pathname, the kernel code searchs in the
  3592. directories to find the corresponding inode number.  After the name has been
  3593. converted to an inode number, the inode is loaded into memory and is used by
  3594. subsequent requests. 
  3595.  
  3596.    Figure A.2 represents a directory.
  3597.  
  3598. A.2.3  Links
  3599.  
  3600.   Unix filesystems implement the concept of link.  Several names can be
  3601. associated with a inode. The inode contains a field containing the number
  3602. associated with the file.  Adding a link simply consists in creating a directory
  3603. entry, where the inode number points to the inode, and in incrementing the links
  3604. count in the inode. When a link is deleted, i.e. when one uses the rm command to
  3605. remove a filename, the kernel decrements the links count and deallocates the
  3606. inode if this count becomes zero.
  3607.  
  3608. A.2.  Basic File System Concepts                                             81
  3609.                          Figure A.2: Structure of a directory  
  3610.  
  3611.    This type of link is called a hard link and can only be used within a single
  3612. filesystem: it is impossible to create cross-filesystem hard links. Moreover,
  3613. hard links can only point on files: a directory hard link cannot be created to
  3614. prevent the apparition of a cycle in the directory tree. 
  3615.  
  3616.    Another kind of links exists in most Unix filesystems. Symbolic links are
  3617. simply files which contain a filename.  When the kernel encounters a symbolic
  3618. link during a pathname to inode conversion, it replaces the name of the link by
  3619. its contents, i.e.  the name of the target file, and restarts the pathname
  3620. interpretation.  Since a symbolic link does not point to an inode, it is
  3621. possible to create cross-filesystems symbolic links. Symbolic links can point to
  3622. any type of file, even on nonexistent files. Symbolic links are very useful
  3623. because they don't have the limitations associated to hard links.  However, they
  3624. use some disk space, allocated for their inode and their data blocks, and cause
  3625. an overhead in the pathname to inode conversion because the kernel has to
  3626. restart the name interpretation when it encounters a symbolic link.
  3627.  
  3628. A.2.4  Device special files
  3629.  
  3630.   In Unix-like operating systems, devices can be accessed via special files.  A
  3631. device special file does not use any space on the filesystem. It is only an
  3632. access point to the device driver. 
  3633.  
  3634.    Two types of special files exist: character and block special files. The
  3635. former allows I/O operations in character mode while the later requires data to
  3636. be written in block mode via the buffer cache functions. When an I/O request is
  3637. made on a special file, it is forwarded to a (pseudo) device driver.  A special
  3638. file is referenced by a major
  3639.  
  3640. 82     Appendix A. Design and Implementation of the Second Extended Filesystem
  3641.  
  3642.   number, which identifies the device type, and a minor number, which identifies
  3643. the unit.
  3644.  
  3645. A.3    The Virtual File System  
  3646.  
  3647. A.3.1  Principle
  3648.  
  3649.   The Linux kernel contains a Virtual File System layer which is used during
  3650. system calls acting on files. The VFS is an indirection layer which handles the
  3651. file oriented system calls and calls the necessary functions in the physical
  3652. filesystem code to do the I/O. 
  3653.  
  3654.    This indirection mechanism is frequently used in Unix-like operating systems
  3655. to ease the integration and the use of several filesystem types [5, 8]. 
  3656.  
  3657.    When a process issues a file oriented system call, the kernel calls a
  3658. function con- tained in the VFS. This function handles the structure independent
  3659. manipulations and redirects the call to a function contained in the physical
  3660. filesystem code, which is responsible for handling the structure dependent
  3661. operations. Filesystem code uses the buffer cache functions to request I/O on
  3662. devices. This scheme is illustrated on figure A.3.
  3663.  
  3664. A.3.2  The VFS structure
  3665.  
  3666.   The VFS defines a set of functions that every filesystem has to implement.
  3667. This inter- face is made up of a set of operations associated to three kinds of
  3668. objects: filesystems, inodes, and open files. 
  3669.  
  3670.    The VFS knows about filesystem types supported in the kernel. It uses a table
  3671. defined during the kernel configuration. Each entry in this table describes a
  3672. filesystem type: it contains the name of the filesystem type and a pointer on a
  3673. function called during the mount operation. When a filesystem is to be mounted,
  3674. the appropriate mount function is called.  This function is responsible for
  3675. reading the superblock from the disk, initializing its internal variables, and
  3676. returning a mounted filesystem descriptor to the VFS. After the filesystem is
  3677. mounted, the VFS functions can use this descriptor to access the physical
  3678. filesystem routines. 
  3679.  
  3680.    A mounted filesystem descriptor contains several kinds of data: informations
  3681. that are common to every filesystem types, pointers to functions provided by the
  3682. physical filesystem kernel code, and private data maintained by the physical
  3683. filesystem code.
  3684.  
  3685. A.4.  The Second Extended File System                                         83 
  3686.  
  3687.  
  3688.                          Figure A.3: The VFS Layer  The function pointers
  3689. contained in the filesystem descriptors allow the VFS to access the filesystem
  3690. internal routines. 
  3691.  
  3692.    Two other types of descriptors are used by the VFS: an inode descriptor and
  3693. an open file descriptor.  Each descriptor contains informations related to files
  3694. in use and a set of operations provided by the physical filesystem code.  While
  3695. the inode descriptor contains pointers to functions that can be used to act on
  3696. any file (e.g. create, unlink), the file descriptors contains pointer to
  3697. functions which can only act on open files (e.g. read, write). 
  3698.  
  3699. A.4    The Second Extended File System
  3700.  
  3701. A.4.1  Motivations
  3702.  
  3703.   The Second Extended File System has been designed and implemented to fix some
  3704. problems present in the first Extended File System. Our goal was to provide a
  3705. pow- erful filesystem, which implements Unix file semantics and offers advanced
  3706. features.
  3707.  
  3708. 84     Appendix A. Design and Implementation of the Second Extended Filesystem
  3709.  
  3710.      Of course, we wanted to Ext2fs to have excellent performance. We also
  3711. wanted to provide a very robust filesystem in order to reduce the risk of data
  3712. loss in intensive use. Last, but not least, Ext2fs had to include provision for
  3713. extensions to allow users to benefit from new features without reformatting
  3714. their filesystem.
  3715.  
  3716.  
  3717. A.4.2  \Standard" Ext2fs features
  3718.  
  3719.  
  3720.   The Ext2fs supports standard Unix file types: regular files, directories,
  3721. device special files and symbolic links. 
  3722.  
  3723.    Ext2fs is able to manage filesystems created on really big partitions. While
  3724. the original kernel code restricted the maximal filesystem size to 2 GB, recent
  3725. work in the VFS layer have raised this limit to 4 TB. Thus, it is now possible
  3726. to use big disks without the need of creating many partitions. 
  3727.  
  3728.    Ext2fs provides long file names.  It uses variable length directory entries. 
  3729. The maximal file name size is 255 characters.  This limit could be extended to
  3730. 1012 if needed. 
  3731.  
  3732.    Ext2fs reserves some blocks for the super user (root). Normally, 5% of the
  3733. blocks are reserved. This allows the administrator to recover easily from
  3734. situations where user processes fill up filesystems.
  3735.  
  3736. A.4.3  \Advanced" Ext2fs features
  3737.  
  3738.  
  3739.   In addition to the standard Unix features, Ext2fs supports some extensions
  3740. which are not usually present in Unix filesystems. 
  3741.  
  3742.    File attributes allow the users to modify the kernel behavior when acting on
  3743. a set of files. One can set attributes on a file or on a directory. In the later
  3744. case, new files created in the directory inherit these attributes. 
  3745.  
  3746.    BSD or System V Release 4 semantics can be selected at mount time. A mount
  3747. option allows the administrator to choose the file creation semantics. On a
  3748. filesystem mounted with BSD semantics, files are created with the same group id
  3749. as their parent directory. System V semantics are a bit more complex: if a
  3750. directory has the setgid bit set, new files inherit the group id of the
  3751. directory and subdirectories inherit the group id and the setgid bit; in the
  3752. other case, files and subdirectories are created with the primary group id of
  3753. the calling process. 
  3754.  
  3755.    BSD-like synchronous updates can be used in Ext2fs.  A mount option allows
  3756. the administrator to request that metadata (inodes, bitmap blocks, indirect
  3757. blocks
  3758.  
  3759. A.4.  The Second Extended File System                                         85
  3760.  
  3761.   and directory blocks) be written synchronously on the disk when they are
  3762. modified. This can be useful to maintain a strict metadata consistency but this
  3763. leads to poor performances.  Actually, this feature is not normally used, since
  3764. in addition to the performance loss associated with using synchronous updates of
  3765. the metadata, it can cause corruption in the user data which will not be flagged
  3766. by the filesystem checker. 
  3767.  
  3768.    Ext2fs allows the administrator to choose the logical block size when
  3769. creating the filesystem. Block sizes can typically be 1024, 2048 and 4096 bytes.
  3770. Using big block sizes can speed up I/O since fewer I/O requests, and thus fewer
  3771. disk head seeks, need to be done to access a file. On the other hand, big blocks
  3772. waste more disk space: on the average, the last block allocated to a file is
  3773. only half full, so as blocks get bigger, more space is wasted in the last block
  3774. of each file. In addition, most of the advantages of larger block sizes are
  3775. obtained by Ext2 filesystem's preallocation techniques (see section A.4.5). 
  3776.  
  3777.    Ext2fs implements fast symbolic links. A fast symbolic link does not use any
  3778. data block on the filesystem. The target name is not stored in a data block but
  3779. in the inode itself. This policy can save some disk space (no data block needs
  3780. to be allocated) and speeds up link operations (there is no need to read a data
  3781. block when accessing such a link). Of course, the space available in the inode
  3782. is limited so not every link can be implemented as a fast symbolic link. The
  3783. maximal size of the target name in a fast symbolic link is 60 characters. We
  3784. plan to extend this scheme to small files in a near future. 
  3785.  
  3786.    Ext2fs keeps track of the filesystem state. A special field in the superblock
  3787. is used by the kernel code to indicate the status of the file system.  When a
  3788. filesystem is mounted in read/write mode, its state is set to \Not Clean". When
  3789. it is unmounted or remounted in read-only mode, its state is reset to \Clean". 
  3790. At boot time, the filesystem checker uses this information to decide if a
  3791. filesystem must be checked. The kernel code also records errors in this field.
  3792. When an inconsistency is detected by the kernel code, the filesystem is marked
  3793. as \Erroneous". The filesystem checker tests this to force the check of the
  3794. filesystem regardless of its apparently clean state. 
  3795.  
  3796.    Always skipping filesystem checks may sometimes be dangerous so Ext2fs
  3797. provides two ways to force checks at regular intervals. A mount counter is
  3798. maintained in the superblock.  Each time the filesystem is mounted in read/write
  3799. mode, this counter is incremented. When it reaches a maximal value (also
  3800. recorded in the superblock), the filesystem checker forces the check even if the
  3801. filesystem is \Clean". A last check time and a maximal check interval are also
  3802. maintained in the superblock.  These two fields allow the administrator to
  3803. request periodical checks. When the maximal check interval has been reached, the
  3804. checker ignores the filesystem state and forces a
  3805.  
  3806. 86     Appendix A. Design and Implementation of the Second Extended Filesystem
  3807.  
  3808.  
  3809.   filesystem check. 
  3810.  
  3811.    Ext2fs offers tools to tune the filesystem behavior. The tune2fs program can
  3812. be used to modify:    othe error behavior. When an inconsistency is detected by
  3813. the kernel code, the     filesystem is marked as \Erroneous" and one of the
  3814. three following actions can     be done: continue normal execution, remount the
  3815. filesystem in read-only mode     to avoid corrupting the filesystem, make the
  3816. kernel panic and reboot to run the     filesystem checker.    othe maximal mount
  3817. count.    othe maximal check interval.    othe number of logical blocks reserved
  3818. for the super user.     Mount options can also be used to change the kernel
  3819. error behavior. 
  3820.  
  3821.    An attribute allows the users to request secure deletion on files. When such
  3822. a file is deleted, random data is written in the disk blocks previously
  3823. allocated to the file. This prevents malicious people from gaining access to the
  3824. previous content of the file by using a disk editor. 
  3825.  
  3826.    Last, new types of files inspired from the 4.4 BSD filesystem have recently
  3827. been added to Ext2fs.  Immutable files can only be read:  nobody can write or
  3828. delete them. This can be used to protect sensitive configuration files.
  3829. Append-only files can be opened in write mode but data is always appended at the
  3830. end of the file.  Like immutable files, they cannot be deleted or renamed. This
  3831. is especially useful for log files which can only grow.
  3832.  
  3833.  
  3834. A.4.4  Physical Structure
  3835.  
  3836.   The physical structure of Ext2 filesystems has been strongly influenced by the
  3837. layout of the BSD filesystem [6]. A filesystem is made up of block groups. Block
  3838. groups are analogous to BSD FFS's cylinder groups. However, block groups are not
  3839. tied to the physical layout of the blocks on the disk, since modern drives tend
  3840. to be optimized for sequential access and hide their physical geometry to the
  3841. operating system. 
  3842.  
  3843.    The physical structure of a filesystem is represented on figure A.4. 
  3844.  
  3845.    Each block group contains a redundant copy of crucial filesystem control
  3846. infor- mations (superblock and the filesystem descriptors) and also contains a
  3847. part of the filesystem (a block bitmap, an inode bitmap, a piece of the inode
  3848. table, and data blocks). The structure of a block group is represented on figure
  3849. A.5.
  3850. A.4.  The Second Extended File System                                         87
  3851.  
  3852.  
  3853.  
  3854.                 ______________________________________
  3855.                 |  Boot  |Block  |Block  |... |Block   |
  3856.                 |        |       |       |    |        |
  3857.                 | SectorG|roup 1 |Group 2 |...G|roup N |
  3858.                 |________|_______|________|____|______ |
  3859.  
  3860.  
  3861.  
  3862.                  Figure A.4: Physical structure of an Ext2 filesystem
  3863.  
  3864.  
  3865.           ___________________________________________________
  3866.           | Super |FS desc- |Block  |Inode I|node |Data Blocks |
  3867.           |       |         |       |       |     |            |
  3868.           | Block |riptors B|itmap |Bitmap |Table |          |
  3869.           |_______|_________|______|_______|______|__________|
  3870.  
  3871.  
  3872.  
  3873.                      Figure A.5: Structure of a block group
  3874.  
  3875.  
  3876.    Using block groups is a big win in terms of reliability: since the control
  3877. structures are replicated in each block group, it is easy to recover from a
  3878. filesystem where the superblock has been corrupted. This structure also helps to
  3879. get good performances: by reducing the distance between the inode table and the
  3880. data blocks, it is possible to reduce the disk head seeks during I/O on files. 
  3881.  
  3882.    In Ext2fs, directories are managed as linked lists of variable length
  3883. entries. Each entry contains the inode number, the entry length, the file name
  3884. and its length. By using variable length entries, it is possible to implement
  3885. long file names without wasting disk space in directories.  The structure of a
  3886. directory entry is shown on
  3887.  
  3888. figure A.6.
  3889.  
  3890.  
  3891.             _______________________________________________
  3892.             | inode number |entry lengthn|ame lengthf|ilename |
  3893.             |______________|_____________|___________|_____   |
  3894.  
  3895.  
  3896.  
  3897.                     Figure A.6: Structure of a directory entry
  3898.  
  3899.  
  3900.  
  3901.    As an example, figure A.7 represents the structure of a directory containing
  3902. three files: file1, long_file_name, and f2.
  3903.  
  3904. A.4.5  Performance optimizations
  3905.  
  3906. The Ext2fs kernel code contains many performance optimizations, which tend to
  3907. improve I/O speed when reading and writing files.
  3908.  
  3909.    Ext2fs takes advantage of the buffer cache management by performing
  3910. readaheads: when a block has to be read, the kernel code requests the I/O on
  3911. several contiguous blocks. This way, it tries to ensure that the next block to
  3912. read will already be loaded into the buffer cache. Readaheads are normally
  3913. performed during sequential reads on
  3914. 88     Appendix A. Design and Implementation of the Second Extended Filesystem
  3915.  
  3916.  
  3917. _____________________________________________________________________
  3918. | i1 |160|5 |file1    || i24|01|4 |long_file_name  ||i3 |120|2 |f2   |
  3919. |____|___|__|_________||____|__|__|________________||___|___|__|_____|
  3920.  
  3921.                         Figure A.7: Example of directory
  3922.  
  3923.  
  3924.  
  3925. files and Ext2fs extends them to directory reads, either explicit reads
  3926. (readdir(2) calls) or implicit ones (namei kernel directory lookup).
  3927.  
  3928.    Ext2fs also contains many allocation optimizations.  Block groups are used to
  3929. cluster together related inodes and data: the kernel code always tries to
  3930. allocate data blocks for a file in the same group as its inode. This is intended
  3931. to reduce the disk head seeks made when the kernel reads an inode and its data
  3932. blocks.
  3933.  
  3934.    When writing data to a file, Ext2fs preallocates up to 8 adjacent blocks when
  3935. allocating a new block.  Preallocation hit rates are around 75% even on very
  3936. full filesystems. This preallocation achieves good write performances under
  3937. heavy load. It also allows contiguous blocks to be allocated to files, thus it
  3938. speeds up the future sequential reads.
  3939.  
  3940.    These two allocation optimizations produce a very good locality of:   
  3941. orelated files through block groups orelated blocks through the 8 bits
  3942. clustering of block allocations.
  3943.  
  3944. A.5    The Ext2fs library
  3945.  
  3946. To allow user mode programs to manipulate the control structures of an Ext2
  3947. filesystem, the libext2fs library was developed.  This library provides routines
  3948. which can be used to examine and modify the data of an Ext2 filesystem, by
  3949. accessing the filesystem directly through the physical device.
  3950.  
  3951.    The Ext2fs library was designed to allow maximal code reuse through the use
  3952. of software abstraction techniques. For example, several different iterators are
  3953. provided. A program can simply pass in a function to ext2fs_block_interate(),
  3954. which will be called for each block in an inode. Another iterator function
  3955. allows an user-provided function to be called for each file in a directory.
  3956.  
  3957.    Many of the Ext2fs utilities (mke2fs, e2fsck, tune2fs, dumpe2fs, and debugfs)
  3958. use the Ext2fs library. This greatly simplifies the maintainance of these
  3959. utilities, since any changes to reflect new features in the Ext2 filesystem
  3960. format need only be made in one place _ in the Ext2fs library. This code reuse
  3961. also results in smaller binaries, since the Ext2fs library can be built as a
  3962. shared library image.
  3963. A.6.  The Ext2fs tools                                                     89
  3964.  
  3965.    Because the interfaces of the Ext2fs library are so abstract and general, new
  3966. programs which require direct access to the Ext2fs filesystem can very easily be
  3967. written. For example, the Ext2fs library was used during the port of the 4.4BSD
  3968. dump and restore backup utilities. Very few changes were needed to adapt these
  3969. tools to Linux: only a few filesystem dependent functions had to be replaced by
  3970. calls to the Ext2fs library.
  3971.  
  3972.    The Ext2fs library provides access to several classes of operations. The
  3973. first class are the filesystem-oriented operations. A program can open and close
  3974. a filesystem, read and write the bitmaps, and create a new filesystem on the
  3975. disk. Functions are also available to manipulate the filesystem's bad blocks
  3976. list.
  3977.  
  3978.    The second class of operations affect directories. A caller of the Ext2fs
  3979. library can create and expand directories, as well as add and remove directory
  3980. entries. Functions are also provided to both resolve a pathname to an inode
  3981. number, and to determine a pathname of an inode given its inode number.
  3982.  
  3983.    The final class of operations are oriented around inodes. It is possible to
  3984. scan the inode table, read and write inodes, and scan through all of the blocks
  3985. in an inode. Allocation and deallocation routines are also available and allow
  3986. user mode programs to allocate and free blocks and inodes.
  3987.  
  3988. A.6    The Ext2fs tools
  3989.  
  3990.  
  3991.  
  3992. Powerful management tools have been developed for Ext2fs. These utilities are
  3993. used to create, modify, and correct any inconsistencies in Ext2 filesystems. The
  3994. mke2fs program is used to initialize a partition to contain an empty Ext2
  3995. filesystem.
  3996.  
  3997.    The tune2fs program can be used to modify the filesystem parameters. As ex-
  3998. plained in section A.4.3, it can change the error behavior, the maximal mount
  3999. count, the maximal check interval, and the number of logical blocks reserved for
  4000. the super user.
  4001.  
  4002.    The most interesting tool is probably the filesystem checker. E2fsck is
  4003. intended to repair filesystem inconsistencies after an unclean shutdown of the
  4004. system.  The original version of e2fsck was based on Linus Torvald's fsck
  4005. program for the Minix filesystem. However, the current version of e2fsck was
  4006. rewritten from scratch, using the Ext2fs library, and is much faster and can
  4007. correct more filesystem inconsistencies than the original version.
  4008.  
  4009.    The e2fsck program is designed to run as quickly as possible.  Since
  4010. filesystem
  4011. 90     Appendix A. Design and Implementation of the Second Extended Filesystem
  4012.  
  4013. checkers tend to be disk bound, this was done by optimizing the algorithms used
  4014. by e2fsck so that filesystem structures are not repeatedly accessed from the
  4015. disk. In addition, the order in which inodes and directories are checked are
  4016. sorted by block number to reduce the amount of time in disk seeks. Many of these
  4017. ideas were originally explored by [3] although they have since been further
  4018. refined by the authors. 
  4019.  
  4020.    In pass 1, e2fsck iterates over all of the inodes in the filesystem and
  4021. performs checks over each inode as an unconnected object in the filesystem. 
  4022. That is, these checks do not require any cross-checks to other filesystem
  4023. objects. Examples of such checks include making sure the file mode is legal, and
  4024. that all of the blocks in the inode are valid block numbers. During pass 1,
  4025. bitmaps indicating which blocks and inodes are in use are compiled. 
  4026.  
  4027.    If e2fsck notices data blocks which are claimed by more than one inode, it
  4028. invokes passes 1B through 1D to resolve these conflicts, either by cloning the
  4029. shared blocks so that each inode has its own copy of the shared block, or by
  4030. deallocating one or more of the inodes. 
  4031.  
  4032.    Pass 1 takes the longest time to execute, since all of the inodes have to be
  4033. read into memory and checked. To reduce the I/O time necessary in future passes,
  4034. critical filesystem information is cached in memory.  The most important example
  4035. of this technique is the location on disk of all of the directory blocks on the
  4036. filesystem. This obviates the need to re-read the directory inodes structures
  4037. during pass 2 to obtain this information. 
  4038.  
  4039.    Pass 2 checks directories as unconnected objects. Since directory entries do
  4040. not span disk blocks, each directory block can be checked individually without
  4041. reference to other directory blocks. This allows e2fsck to sort all of the
  4042. directory blocks by block number, and check directory blocks in ascending order,
  4043. thus decreasing disk seek time. The directory blocks are checked to make sure
  4044. that the directory entries are valid, and contain references to inode numbers
  4045. which are in use (as determined by pass 1). 
  4046.  
  4047.    For the first directory block in each directory inode, the `.'  and `..' 
  4048. entries are checked to make sure they exist, and that the inode number for the
  4049. `.' entry matches the current directory. (The inode number for the `..' entry is
  4050. not checked until pass 3.) 
  4051.  
  4052.    Pass 2 also caches information concerning the parent directory in which each
  4053. di- rectory is linked. (If a directory is referenced by more than one directory,
  4054. the second reference of the directory is treated as an illegal hard link, and it
  4055. is removed). 
  4056.  
  4057.    It is noteworthy to note that at the end of pass 2, nearly all of the disk
  4058. I/O which
  4059. A.7.  Performance Measurements                                             91
  4060.  
  4061. e2fsck needs to perform is complete. Information required by passes 3, 4 and 5
  4062. are cached in memory; hence, the remaining passes of e2fsck are largely CPU
  4063. bound, and take less than 5-10% of the total running time of e2fsck.
  4064.  
  4065.    In pass 3, the directory connectivity is checked. E2fsck traces the path of
  4066. each directory back to the root, using information that was cached during pass
  4067. 2. At this time, the `..' entry for each directory is also checked to make sure
  4068. it is valid. Any directories which can not be traced back to the root are linked
  4069. to the /lost+found directory.
  4070.  
  4071.    In pass 4, e2fsck checks the reference counts for all inodes, by iterating
  4072. over all the inodes and comparing the link counts (which were cached in pass 1)
  4073. against internal counters computed during passes 2 and 3. Any undeleted files
  4074. with a zero link count is also linked to the /lost+found directory during this
  4075. pass.
  4076.  
  4077.    Finally, in pass 5, e2fsck checks the validity of the filesystem summary
  4078. informa- tion.  It compares the block and inode bitmaps which were constructed
  4079. during the previous passes against the actual bitmaps on the filesystem, and
  4080. corrects the on-disk copies if necessary.
  4081.  
  4082.    The filesystem debugger is another useful tool.  Debugfs is a powerful
  4083. program which can be used to examine and change the state of a filesystem. 
  4084. Basically, it provides an interactive interface to the Ext2fs library: commands
  4085. typed by the user are translated into calls to the library routines.
  4086.  
  4087.    Debugfs can be used to examine the internal structures of a filesystem,
  4088. manually repair a corrupted filesystem, or create test cases for e2fsck. 
  4089. Unfortunately, this program can be dangerous if it is used by people who do not
  4090. know what they are doing; it is very easy to destroy a filesystem with this
  4091. tool. For this reason, debugfs opens filesytems for read-only access by default.
  4092. The user must explicitly specify the -w flag in order to use debugfs to open a
  4093. filesystem for read/wite access.
  4094.  
  4095. A.7    Performance Measurements
  4096.  
  4097.  
  4098.  
  4099. A.7.1  Description of the benchmarks
  4100.  
  4101. We have run benchmarks to measure filesystem performances. Benchmarks have been
  4102. made on a middle-end PC, based on a i486DX2 processor, using 16 MB of memory and
  4103. two 420 MB IDE disks. The tests were run on Ext2 fs and Xia fs (Linux 1.1.62)
  4104. and on the BSD Fast filesystem in asynchronous and synchronous mode (FreeBSD 2.0
  4105. Alpha _ based on the 4.4BSD Lite distribution).
  4106. 92     Appendix A. Design and Implementation of the Second Extended Filesystem
  4107.  
  4108.     We have run two different benchmarks. The Bonnie benchmark tests I/O speed
  4109. on a big file _ the file size was set to 60 MB during the tests. It writes data
  4110. to the file using character based I/O, rewrites the contents of the whole file,
  4111. writes data using block based I/O, reads the file using character I/O and block
  4112. I/O, and seeks into the file. The Andrew Benchmark was developed at Carneggie
  4113. Mellon University and has been used at the University of Berkeley to benchmark
  4114. BSD FFS and LFS. It runs in five phases: it creates a directory hierarchy, makes
  4115. a copy of the data, recursively examine the status of every file, examine every
  4116. byte of every file, and compile several of the files.
  4117.  
  4118. A.7.2  Results of the Bonnie benchmark
  4119.  
  4120. The results of the Bonnie benchmark are presented in table A.2.
  4121.  
  4122.  
  4123.  
  4124.                     Table A.2: Results of the Bonnie benchmark
  4125.          _____________________________________________________
  4126.          |            |Char   |Block  |Rewrite|Char   | Block|
  4127.          |            |       |       |       |       |      |
  4128.          |            |Write  |Write  |       | Read  | Read |
  4129.          |            |       |       |       |       |      |
  4130.          |            |(KB/s) |(KB/s) |(KB/s) |(KB/s) |(KB/s)|
  4131.          |____________|_______|_______|_______|_______|______|
  4132.          | BSD Async  | 710   |  684  | 401   |  721  | 888  |
  4133.          |            |       |       |       |       |      |
  4134.          | BSD Sync   | 699   |  677  | 400   |  710  | 878  |
  4135.          |            |       |       |       |       |      |
  4136.          | Ext2 fs    |452    |1237   | 536   |  397  |1033  |
  4137.          |            |       |       |       |       |      |
  4138.          | Xia fs     |440    | 704   | 380   |  366  | 895  |
  4139.          |____________|_______|_______|_______|_______|______|
  4140.    The results are very good in block oriented I/O: Ext2 fs outperforms other
  4141. filesystems. This is clearly a benefit of the optimizations included in the
  4142. allocation routines. Writes are fast because data is written in cluster mode.
  4143. Reads are fast because contiguous blocks have been allocated to the file. Thus
  4144. there is no head seek between two reads and the readahead optimizations can be
  4145. fully used.
  4146.  
  4147.    On the other hand, performance is better in the FreeBSD operating system in
  4148. character oriented I/O. This is probably due to the fact that FreeBSD and Linux
  4149. do not use the same stdio routines in their respective C libraries. It seems
  4150. that FreeBSD has a more optimized character I/O library and its performance is
  4151. better.
  4152.  
  4153. A.7.3  Results of the Andrew benchmark
  4154.  
  4155.  
  4156.  
  4157. The results of the Andrew benchmark are presented in table A.3.
  4158. A.8.  Conclusion                                                         93
  4159.  
  4160.  
  4161.  
  4162.  
  4163.                    Table A.3: Results of the Andrew benchmark
  4164.             _______________________________________________
  4165.             |            |P1    | P2  | P3  | P4   |   P5  |
  4166.             |            |      |     |     |      |       |
  4167.             |            |Create|Copy |Stat |Grep  |Compile|
  4168.             |            |      |     |     |      |       |
  4169.             |            |(ms)  |(ms) |(ms) |(ms)  |(ms)   |
  4170.             |____________|______|_____|_____|______|_______|
  4171.             | BSD Async  | 2203 |7391 |6319 |17466 |75314  |
  4172.             |            |      |     |     |      |       |
  4173.             | BSD Sync   | 2330 |7732 |6317 |17499 |75681  |
  4174.             |            |      |     |     |      |       |
  4175.             | Ext2 fs    |790   |4791 |7235 |11685 |63210  |
  4176.             |            |      |     |     |      |       |
  4177.             | Xia fs     |934   |5402 |8400 |12912 |66997  |
  4178.             |____________|______|_____|_____|______|_______|
  4179.    The results of the two first passes show that Linux benefits from its
  4180. asynchronous metadata I/O. In passes 1 and 2, directories and files are created
  4181. and BSD syn- chronously writes inodes and directory entries. There is an
  4182. anomaly, though: even in asynchronous mode, the performance under BSD is poor. 
  4183. We suspect that the asynchronous support under FreeBSD is not fully implemented.
  4184.  
  4185.    In pass 3, the Linux and BSD times are very similar. This is a big progress
  4186. against the same benchmark run six months ago. While BSD used to outperform
  4187. Linux by a factor of 3 in this test, the addition of a file name cache in the
  4188. VFS has fixed this performance problem.
  4189.  
  4190.    In passes 4 and 5, Linux is faster than FreeBSD mainly because it uses an
  4191. unified buffer cache management. The buffer cache space can grow when needed and
  4192. use more memory than the one in FreeBSD, which uses a fixed size buffer cache.
  4193. Comparison of the Ext2fs and Xiafs results shows that the optimizations included
  4194. in Ext2fs are really useful: the performance gain between Ext2fs and Xiafs is
  4195. around 5-10 %.
  4196.  
  4197. A.8    Conclusion
  4198.  
  4199. The Second Extended File System is probably the most widely used filesystem in
  4200. the Linux community. It provides standard Unix file semantics and advanced
  4201. features. Moreover, thanks to the optimizations included in the kernel code, it
  4202. is robust and offers excellent performance.
  4203.  
  4204.    Since Ext2fs has been designed with evolution in mind, it contains hooks that
  4205. can be used to add new features. Some people are working on extensions to the
  4206. current filesystem: access control lists conforming to the Posix semantics [7],
  4207. undelete, and on the fly file compression.
  4208. 94     Appendix A. Design and Implementation of the Second Extended Filesystem
  4209.  
  4210.    Ext2fs was first developed and integrated in the Linux kernel and is now
  4211. actively being ported to other operating systems. An Ext2fs server running on
  4212. top of the GNU Hurd has been implemented. People are also working on an Ext2fs
  4213. port in the LITES server, running on top of the Mach microkernel [1], and in the
  4214. VSTa operating system. Last, but not least, Ext2fs is an important part of the
  4215. Masix operating system [4], currently under development by one of the authors.
  4216.  
  4217. Acknowledgments
  4218.  
  4219. The Ext2fs kernel code and tools have been written mostly by the authors of this
  4220. paper. Some other people have also contributed to the development of Ext2fs
  4221. either by suggesting new features or by sending patches. We want to thank these
  4222. contributors for their help.
  4223.  
  4224.  
  4225. Bibliography
  4226.  
  4227. [1]  M. Accetta, R. Baron, W. Bolosky, D. Golub, R. Rashid A. Tevanian, and   
  4228.      M. Young. Mach: A New Kernel Foundation For UNIX Development. In Pro-   
  4229.      ceedings of the USENIX 1986 Summer Conference, June 1986. 
  4230.  
  4231. [2]  M. Bach. The Design of the UNIX Operating System. Prentice Hall, 1986. 
  4232.  
  4233. [3]  E. Bina and P. Emrath.  A Faster fsck for BSD Unix.  In Proceedings of the  
  4234.       USENIX Winter Conference, January 1986. 
  4235.  
  4236. [4]  R. Card, E. Commelin, S. Dayras, and F. M'evel. The MASIX Multi-Server
  4237.      Operating System.  In OSF Workshop on Microkernel Technology for
  4238.      Distributed Systems, June 1993. 
  4239.  
  4240. [5]  S. Kleiman.  Vnodes:  An Architecture for Multiple File System Types in Sun
  4241.      UNIX. In Proceedings of the Summer USENIX Conference, pages 260-269, June
  4242.      1986. 
  4243.  
  4244. [6]  M. McKusick, W. Joy, S. Leffler, and R. Fabry. A Fast File System for UNIX.
  4245.      ACM Transactions on Computer Systems, 3:181-197, August 1984. 
  4246.  
  4247. [7]  Institute of Electrical and Inc Electronics Engineers.  Security interface
  4248.      for the portable operating system interface for computer environments -
  4249.      draft 13, 1992. 
  4250.  
  4251. [8]  M. Seltzer, K. Bostic, M. McKusick, and C. Staelin.  An Implementation of a
  4252.      Log-Structured File System for UNIX.  In Proceedings of the USENIX Winter
  4253.      Conference, January 1993. 
  4254.  
  4255. [9]  A. Tanenbaum. Operating Systems: Design and Implementation. Prentice Hall,
  4256.      1987.
  4257.  
  4258.  
  4259.  
  4260.  
  4261.                                   95
  4262. 96                                                       BIBLIOGRAPHY
  4263.  
  4264.  
  4265.  
  4266.  
  4267.  
  4268. Appendix  B
  4269.  
  4270.  
  4271.  
  4272. Measuring  Holes
  4273.  
  4274.  
  4275.  
  4276. This appendix contains the interesting part of the program used to measure the
  4277. potential for holes in a filesystem. The source distribution of the book
  4278. contains the full source code (sag/measure-holes/measure-holes.c).
  4279.  
  4280.     int process(FILE *f, char *filename) -
  4281.           static char *buf = NULL;
  4282.           static long prev`block`size = -1;
  4283.           long zeroes;
  4284.           char *p;
  4285.  
  4286.           if (buf == NULL __ prev`block`size != block`size) -
  4287.                 free(buf);
  4288.                 buf = xmalloc(block`size + 1);
  4289.                 buf[block`size] = 1;
  4290.                 prev`block`size = block`size;
  4291.           "
  4292.           zeroes = 0;
  4293.           while (fread(buf, block`size, 1, f) == 1) -
  4294.                 for (p = buf; *p == '\0'; )
  4295.                        ++p;
  4296.                 if (p == buf+block`size)
  4297.                        zeroes += block`size;
  4298.           "
  4299.           if (zeroes > 0)
  4300.                 printf("%ld %s\n", zeroes, filename);
  4301.           if (ferror(f)) -
  4302.                 errormsg(0, -1, "read failed for `%s'", filename);
  4303.                 return -1;
  4304.           "
  4305.           return 0;
  4306.     "
  4307.  
  4308.  
  4309.  
  4310.                                   97
  4311. 98                                            Appendix B. Measuring Holes
  4312.  
  4313.  
  4314.  
  4315.  
  4316.  
  4317. Appendix  C
  4318.  
  4319.  
  4320.  
  4321. The  Linux  Device  List
  4322.  
  4323.  
  4324.  
  4325. This is the device list, maintained by H. Peter Anvin (Peter.Anvin@linux.org),
  4326. at ftp://ftp.yggdrasil.com/pub/device-list/devices.tex.  The rest of this text
  4327. is by Peter.
  4328.  
  4329. C.1    Introduction
  4330.  
  4331. This list is the successor to Rick Miller's Linux Device List, which he stopped
  4332. main- taining when he lost network access in 1993. It is a registry of allocated
  4333. major device numbers, as well as the recommended /dev directory nodes for these
  4334. devices.
  4335.  
  4336.    This list is available via FTP from ftp.yggdrasil.com in the directory
  4337. /pub/device-list; filename is devices.format where format is txt (ASCII), tex
  4338. (LATEX), dvi (DVI) or ps (PostScript). In cases of discrepancy, the LATEX
  4339. version has priority.
  4340.  
  4341.    This document is included by reference into the Linux Filesystem Standard
  4342. (FSSTND). The FSSTND is available via FTP from tsx-11.mit.edu in the directory
  4343. /pub/linux/docs/linux-standards/fsstnd.
  4344.  
  4345.    To have a major number allocated, or a minor number in situations where that
  4346. applies (e.g. busmice), please contact me. Also, if you have additional
  4347. information regarding any of the devices listed below, I would like to know.
  4348.  
  4349.    Allocations marked (68k) apply to Linux/68k only.
  4350.                                   99
  4351. 100                                      Appendix C. The Linux Device List
  4352.  
  4353.  
  4354.  
  4355. C.2    Major numbers
  4356.  
  4357.  
  4358.   0             Unnamed devices (NFS mounts, loopback devices)
  4359.   1     char    Memory devices
  4360.         block   RAM disk
  4361.  
  4362.   2     char    Reserved for PTY's <tytso@athena.mit.edu>
  4363.         block   Floppy disks
  4364.  
  4365.   3     char    Reserved for PTY's <tytso@athena.mit.edu>
  4366.         block   First MFM, RLL and IDE hard disk/CD-ROM interface
  4367.  
  4368.   4     char    TTY devices
  4369.  
  4370.   5     char    Alternate TTY devices
  4371.  
  4372.   6     char    Parallel printer devices
  4373.  
  4374.   7     char    Virtual console access devices
  4375.  
  4376.   8     block   SCSI disk devices
  4377.  
  4378.   9     char    SCSI tape devices
  4379.         block   Multiple disk devices
  4380.  
  4381.  10     char    Non-serial mice, misc features
  4382.  
  4383.  11     block   SCSI CD-ROM devices
  4384.  
  4385.  12     char    QIC-02 tape
  4386.         block   MSCDEX CD-ROM callback support
  4387.  
  4388.  13     char    PC speaker
  4389.         block   8-bit MFM/RLL/IDE controller
  4390.  
  4391.  14     char    Sound card
  4392.         block   BIOS harddrive callback support
  4393.  
  4394.  15     char    Joystick
  4395.         block   Sony CDU-31A/CDU-33A CD-ROM
  4396.  
  4397.  16     char    Reserved for scanners
  4398.         block   GoldStar CD-ROM
  4399.  
  4400.  17     char    Chase serial card (Under development)
  4401.         block   Optics Storage CD-ROM (Under development)
  4402.  
  4403.  18     char    Chase serial card - alternate devices
  4404.         block   Sanyo CD-ROM (Under development)
  4405.  
  4406.  19     char    Cyclades serial card
  4407.         block   Double compressed disk
  4408.  
  4409.  20     char    Cyclades serial card - alternate devices
  4410.         block   Hitachi CD-ROM (Under development)
  4411.  
  4412.  21     char    Generic SCSI access
  4413.  
  4414.  22     char    Digiboard serial card
  4415.      C.3.  Minor numbers                                                     101
  4416.  
  4417.  
  4418.  
  4419.             block   Second MFM, RLL and IDE hard disk/CD-ROM interface
  4420.  
  4421.       23    char    Digiboard serial card - alternate devices
  4422.             block   Mitsumi proprietary CD-ROM
  4423.  
  4424.       24    char    Stallion serial card
  4425.             block   Sony CDU-535 CD-ROM
  4426.  
  4427.       25    char    Stallion serial card - alternate devices
  4428.             block   First Matsushita (Panasonic/SoundBlaster) CD-ROM
  4429.  
  4430.       26    block   Second Matsushita (Panasonic/SoundBlaster) CD-ROM
  4431.  
  4432.       27    char    QIC-117 tape
  4433.             block   Third Matsushita (Panasonic/SoundBlaster) CD-ROM
  4434.  
  4435.       28    char    Stallion serial card - card programming
  4436.             block   Fourth Matsushita (Panasonic/SoundBlaster) CD-ROM
  4437.             block   ACSI disk (68k)
  4438.  
  4439.       29    char    Universal frame buffer
  4440.             block   Aztech/Orchid/Okano/Wearnes CD-ROM
  4441.  
  4442.       30    char    iBCS-2
  4443.             block   Philips LMS-205 CD-ROM
  4444.  
  4445.       31    char    MPU-401 MIDI
  4446.             block   ROM/flash memory card
  4447.  
  4448.       32    block   Philips LMS-206 CD-ROM
  4449.  
  4450.       33    block   Modular RAM disk
  4451.  
  4452.  34-223             Unallocated
  4453.  
  4454. 224-254             Local use
  4455.  
  4456.      255            Reserved
  4457.  
  4458.  
  4459.  
  4460.      C.3    Minor numbers
  4461.  
  4462.        0            Unnamed devices (NFS mounts, loopback devices)
  4463.                       0                  reserved as null device number
  4464.  
  4465.        1    char    Memory devices
  4466.                       1 /dev/mem         Physical memory access
  4467.                       2 /dev/kmem        Kernel virtual memory access
  4468.                       3 /dev/null        Null device
  4469.                       4 /dev/port        I/O port access
  4470. 102                                      Appendix C. The Linux Device List
  4471.  
  4472.                  5 /dev/zero        Null byte source
  4473.                  6 /dev/core        OBSOLETE - should be a link to /proc/kcore
  4474.                  7 /dev/full        Returns ENOSPC on write
  4475.  
  4476.         block   RAM disk
  4477.  
  4478.                  1 /dev/ramdisk     RAM disk
  4479.  
  4480.   2     char    Reserved for PTY's <tytso@athena.mit.edu>
  4481.  
  4482.         block   Floppy disks
  4483.                  0 /dev/fd0         Controller 1, drive 1 autodetect
  4484.                  1 /dev/fd1         Controller 1, drive 2 autodetect
  4485.                  2 /dev/fd2         Controller 1, drive 3 autodetect
  4486.                  3 /dev/fd3         Controller 1, drive 4 autodetect
  4487.                 128/dev/fd4         Controller 2, drive 1 autodetect
  4488.                 129/dev/fd5         Controller 2, drive 2 autodetect
  4489.                 130/dev/fd6         Controller 2, drive 3 autodetect
  4490.                 131/dev/fd7         Controller 2, drive 4 autodetect
  4491.  
  4492.                 To specify format, add to the autodetect device number
  4493.                  0 /dev/fd?         Autodetect format
  4494.                  4 /dev/fd?d360     5.25"  360K in a  360K drive1
  4495.                  20/dev/fd?h360     5.25"  360K in a 1200K drive1
  4496.                  48/dev/fd?h410     5.25"  410K in a 1200K drive
  4497.                  64/dev/fd?h420     5.25"  420K in a 1200K drive
  4498.                  24/dev/fd?h720     5.25"  720K in a 1200K drive
  4499.                  80/dev/fd?h880     5.25"  880K in a 1200K drive1
  4500.                  8 /dev/fd?h1200    5.25" 1200K in a 1200K drive1
  4501.                  40/dev/fd?h1440    5.25" 1440K in a 1200K drive1
  4502.                  56/dev/fd?h1476    5.25" 1476K in a 1200K drive
  4503.                  72/dev/fd?h1494    5.25" 1494K in a 1200K drive
  4504.                  92/dev/fd?h1600    5.25" 1600K in a 1200K drive1
  4505.  
  4506.                  12/dev/fd?u360     3.5"  360K Double Density
  4507.                  16/dev/fd?u720     3.5"  720K Double Density1
  4508.                 120/dev/fd?u800     3.5"  800K Double Density2
  4509.                  52/dev/fd?u820     3.5"  820K Double Density
  4510.                  68/dev/fd?u830     3.5"  830K Double Density
  4511. C.3.  Minor numbers                                                     103
  4512.  
  4513.                 84 /dev/fd?u1040    3.5" 1040K Double Density1
  4514.                 88 /dev/fd?u1120    3.5" 1120K Double Density1
  4515.                 28 /dev/fd?u1440    3.5" 1440K High Density1
  4516.                124 /dev/fd?u1600    3.5" 1600K High Density1
  4517.                 44 /dev/fd?u1680    3.5" 1680K High Density3
  4518.                 60 /dev/fd?u1722    3.5" 1722K High Density
  4519.                 76 /dev/fd?u1743    3.5" 1743K High Density
  4520.                 96 /dev/fd?u1760    3.5" 1760K High Density
  4521.                116 /dev/fd?u1840    3.5" 1840K High Density3
  4522.                100 /dev/fd?u1920    3.5" 1920K High Density1
  4523.                 32 /dev/fd?u2880    3.5" 2880K Extra Density1
  4524.                104 /dev/fd?u3200    3.5" 3200K Extra Density
  4525.                108 /dev/fd?u3520    3.5" 3520K Extra Density
  4526.                112 /dev/fd?u3840    3.5" 3840K Extra Density1
  4527.  
  4528.                 36 /dev/fd?CompaQ   Compaq 2880K drive; probably obsolete
  4529.  
  4530.                1 Autodetectable format
  4531.                2 Autodetectable format in a Double Density (720K) drive only
  4532.                3 Autodetectable format in a High Density (1440K) drive only
  4533.  
  4534.    NOTE: The letter in the device name (d, q, h or u) signifies the type of
  4535. drive supported: 5.25" Double Density (d), 5.25" Quad Density (q), 5.25" High
  4536. Density (h) or 3.5" (any type, u). The capital letters D, H, or E for the 3.5"
  4537. models have been deprecated, since the drive type is insignificant for these
  4538. devices.
  4539.  
  4540.  
  4541.   3    char    Reserved for PTY's <tytso@athena.mit.edu>
  4542.  
  4543.        block   First MFM, RLL and IDE hard disk/CD-ROM interface
  4544.                  0 /dev/hda         Master: whole disk (or CD-ROM)
  4545.                 64 /dev/hdb         Slave: whole disk (or CD-ROM)
  4546.  
  4547.                For partitions, add to the whole disk device number
  4548.                  0 /dev/hd?         Whole disk
  4549.                  1 /dev/hd?1        First primary partition
  4550.                  2 /dev/hd?2        Second primary partition
  4551.                  3 /dev/hd?3        Third primary partition
  4552.                  4 /dev/hd?4        Fourth primary partition 104                                      Appendix C. The Linux Device List
  4553.  
  4554.  
  4555.                  5 /dev/hd?5        First logical partition
  4556.                  6 /dev/hd?6        Second logical partition
  4557.                  7 /dev/hd?7        Third logical partition
  4558.                    : : :
  4559.                  63/dev/hd?63       59th logical partition
  4560.  
  4561.   4     char    TTY devices
  4562.  
  4563.                  0 /dev/console     Console device
  4564.                  1 /dev/tty1        First virtual console
  4565.                    : : :
  4566.                  63/dev/tty63       63rd virtual console
  4567.                  64/dev/ttyS0       First serial port
  4568.                    : : :
  4569.                 127/dev/ttyS63      64th serial port
  4570.                 128/dev/ptyp0       First pseudo-tty master
  4571.                    : : :
  4572.                 191/dev/ptysf       64th pseudo-tty master
  4573.                 192/dev/ttyp0       First pseudo-tty slave
  4574.                    : : :
  4575.                 255/dev/ttysf       64th pseudo-tty slave
  4576.  
  4577. Pseudo-tty's are named as follows:
  4578.  
  4579. o  Masters are pty, slaves are tty;
  4580.  
  4581. o  the fourth letter is one of pqrs indicating the 1st, 2nd, 3rd, 4th series of
  4582.    16 pseudo-ttys each, and
  4583.  
  4584. o  the fifth letter is one of 0123456789abcdef indicating the position within
  4585.    the series.
  4586.  
  4587.  
  4588.   5     char    Alternate TTY devices
  4589.                  0 /dev/tty         Current TTY device
  4590.                  64/dev/cua0        Callout device corresponding to ttyS0
  4591.                    : : :
  4592.                 127/dev/cua63       Callout device corresponding to ttyS63
  4593. C.3.  Minor numbers                                                     105
  4594.  
  4595.   6    char    Parallel printer devices
  4596.  
  4597.                  0 /dev/lp0         First parallel printer (0x3bc)
  4598.                  1 /dev/lp1         Second parallel printer (0x378)
  4599.                  2 /dev/lp2         Third parallel printer (0x278)
  4600.  
  4601. Not all computers have the 0x3bc parallel port, hence the "first" printer may be
  4602. either /dev/lp0 or /dev/lp1.
  4603.  
  4604.  
  4605.   7    char    Virtual console access devices
  4606.  
  4607.                  0 /dev/vcs         Current vc text access
  4608.                  1 /dev/vcs1        tty1 text access
  4609.                    : : :
  4610.                 63 /dev/vcs63       tty63 text access
  4611.                128 /dev/vcsa        Current vc text/attribute access
  4612.                129 /dev/vcsa1       tty1 text/attribute access
  4613.                    : : :
  4614.                191 /dev/vcsa63      tty63 text/attribute access
  4615.  
  4616. NOTE: These devices permit both read and write access.
  4617.  
  4618.   8    block   SCSI disk devices
  4619.  
  4620.                  0 /dev/sda         First SCSI disk whole disk
  4621.                 16 /dev/sdb         Second SCSI disk whole disk
  4622.                 32 /dev/sdc         Third SCSI disk whole disk
  4623.                    : : :
  4624.                240 /dev/sdp         Sixteenth SCSI disk whole disk
  4625.  
  4626. Partitions are handled in the same way as for IDE disks (see major number 3)
  4627. except that the limit on logical partitions is 11 rather than 59 per disk.
  4628.  
  4629.  
  4630.   9    char    SCSI tape devices
  4631.  
  4632.                  0 /dev/st0         First SCSI tape
  4633.                  1 /dev/st1         Second SCSI tape
  4634.                    : : :
  4635.                128 /dev/nst0        First SCSI tape, no rewind-on-close
  4636.                129 /dev/nst1        Second SCSI tape, no rewind-on-close
  4637. 106                                      Appendix C. The Linux Device List
  4638.  
  4639.  
  4640.                    : : :
  4641.  
  4642.         block   Multiple disk devices
  4643.  
  4644.                  0 /dev/md0         First device group
  4645.                  1 /dev/md1         Second device group
  4646.                    : : :
  4647.  
  4648. The multiple device driver is used to span a filesystem across multiple physical
  4649. disks.
  4650.  
  4651.  10     char    Non-serial mice, misc features
  4652.  
  4653.                  0 /dev/logibm      Logitech bus mouse
  4654.                  1 /dev/psaux       PS/2-style mouse port
  4655.                  2 /dev/inportbm    Microsoft Inport bus mouse
  4656.                  3 /dev/atibm       ATI XL bus mouse
  4657.                  4 /dev/jbm         J-mouse
  4658.                  4 /dev/amigamouse  Amiga Mouse (68k)
  4659.                  5 /dev/atarimouse  Atari Mouse (68k)
  4660.                 128/dev/beep        Fancy beep device
  4661.                 129/dev/modreq      Kernel module load request
  4662.  
  4663.  11     block   SCSI CD-ROM devices
  4664.  
  4665.                  0 /dev/sr0         First SCSI CD-ROM
  4666.                  1 /dev/sr1         Second SCSI CD-ROM
  4667.                    : : :
  4668.  
  4669. The prefix /dev/scd instead of /dev/sr has been used as well, and might make
  4670. more sense.
  4671.  
  4672.  
  4673.  12     char    QIC-02 tape
  4674.  
  4675.                  2 /dev/ntpqic11    QIC-11, no rewind-on-close
  4676.                  3 /dev/tpqic11     QIC-11, rewind-on-close
  4677.                  4 /dev/ntpqic24    QIC-24, no rewind-on-close
  4678.                  5 /dev/tpqic24     QIC-24, rewind-on-close
  4679.                  6 /dev/ntpqic120   QIC-120, no rewind-on-close
  4680.                  7 /dev/tpqic120    QIC-120, rewind-on-close
  4681.                  8 /dev/ntpqic150   QIC-150, no rewind-on-close
  4682. C.3.  Minor numbers                                                     107
  4683.  
  4684.                  9 /dev/tpqic150    QIC-150, rewind-on-close
  4685.  
  4686. The device names specified are proposed - if there are \standard" names for
  4687. these devices, please let me know.
  4688.  
  4689.  
  4690.        block   MSCDEX CD-ROM callback support
  4691.  
  4692.                  0 /dev/dos_cd0     First MSCDEX CD-ROM
  4693.                  1 /dev/dos_cd1     Second MSCDEX CD-ROM
  4694.                    : : :
  4695.  
  4696.  13    char    PC speaker
  4697.  
  4698.                  0 /dev/pcmixer     Emulates /dev/mixer
  4699.                  3 /dev/pcsp        Emulates /dev/dsp (8-bit)
  4700.                  4 /dev/pcaudio     Emulates /dev/audio
  4701.                  5 /dev/pcsp16      Emulates /dev/dsp (16-bit)
  4702.  
  4703.        block   8-bit MFM/RLL/IDE controller
  4704.  
  4705.                  0 /dev/xda         First XT disk whole disk
  4706.                 64 /dev/xdb         Second XT disk whole disk
  4707.  
  4708. Partitions are handled in the same way as IDE disks (see major number 3).
  4709.  
  4710.  14    char    Sound card
  4711.  
  4712.                  0 /dev/mixer       Mixer control
  4713.                  1 /dev/sequencer   Audio sequencer
  4714.                  2 /dev/midi00      First MIDI port
  4715.                  3 /dev/dsp         Digital audio
  4716.                  4 /dev/audio       Sun-compatible digital audio
  4717.                  6 /dev/sndstat     Sound card status information
  4718.                  8 /dev/sequencer2  Sequencer - alternate device
  4719.                 16 /dev/mixer1      Second soundcard mixer control
  4720.                 17 /dev/patmgr0     Sequencer patch manager
  4721.                 18 /dev/midi01      Second MIDI port
  4722.                 19 /dev/dsp1        Second soundcard digital audio
  4723.                 20 /dev/audio1      Second soundcard Sun digital audio
  4724.                 33 /dev/patmgr1     Sequencer patch manager
  4725. 108                                      Appendix C. The Linux Device List
  4726.  
  4727.                  34/dev/midi02      Third MIDI port
  4728.                  50/dev/midi03      Fourth MIDI port
  4729.  
  4730.         block   BIOS harddrive callback support
  4731.  
  4732.                  0 /dev/dos_hda     First BIOS harddrive whole disk
  4733.                  64/dev/dos_hdb     Second BIOS harddrive whole disk
  4734.                 128/dev/dos_hdc     Third BIOS harddrive whole disk
  4735.                 192/dev/dos_hdd     Fourth BIOS harddrive whole disk
  4736.  
  4737. Partitions are handled in the same way as IDE disks (see major number 3).
  4738.  
  4739.  15     char    Joystick
  4740.  
  4741.                  0 /dev/js0         First joystick
  4742.                  1 /dev/js1         Second joystick
  4743.  
  4744.         block   Sony CDU-31A/CDU-33A CD-ROM
  4745.  
  4746.                  0 /dev/sonycd      Sony CDU-31A CD-ROM
  4747.  
  4748.  16     char    Reserved for scanners
  4749.  
  4750.         block   GoldStar CD-ROM
  4751.  
  4752.                  0 /dev/gscd        GoldStar CD-ROM
  4753.  
  4754.  17     char    Chase serial card (Under development)
  4755.  
  4756.                  0 /dev/ttyH0       First Chase port
  4757.                  1 /dev/ttyH1       Second Chase port
  4758.                    : : :
  4759.  
  4760.  
  4761.  
  4762.         block   Optics Storage CD-ROM (Under development)
  4763.  
  4764.                  0 /dev/optcd       Optics Storage CD-ROM
  4765.  
  4766.  18     char    Chase serial card - alternate devices
  4767.  
  4768.                  0 /dev/cuh0        Callout device corresponding to ttyH0
  4769.                  1 /dev/cuh1        Callout device corresponding to ttyH1
  4770.                    : : :
  4771. C.3.  Minor numbers                                                     109
  4772.  
  4773.  
  4774.  
  4775.        block   Sanyo CD-ROM (Under development)
  4776.  
  4777.                  0 ?                Sanyo CD-ROM
  4778.  
  4779.  19    char    Cyclades serial card
  4780.  
  4781.                 32 /dev/ttyC0       First Cyclades port
  4782.                    : : :
  4783.                 63 /dev/ttyC31      32nd Cyclades port
  4784.  
  4785. It would make more sense for these to start at 0...
  4786.  
  4787.        block   \Double" compressed disk
  4788.  
  4789.                  0 /dev/double0     First compressed disk
  4790.                    : : :
  4791.                  7 /dev/double7     Eighth compressed disk
  4792.                128 /dev/cdouble0    Mirror of first compressed disk
  4793.                    : : :
  4794.                135 /dev/cdouble7    Mirror of eighth compressed disk
  4795.  
  4796. See the Double documentation for an explanation of the \mirror" devices.
  4797.  
  4798.  20    char    Cyclades serial card - alternate devices
  4799.  
  4800.                 32 /dev/cub0        Callout device corresponding to ttyC0
  4801.                    : : :
  4802.                 63 /dev/cub31       Callout device corresponding to ttyC31
  4803.  
  4804.        block   Hitachi CD-ROM (Under development)
  4805.  
  4806.                  0 /dev/hitcd       Hitachi CD-ROM
  4807.  
  4808.  21    char    Generic SCSI access
  4809.  
  4810.                  0 /dev/sg0         First generic SCSI device
  4811.                  1 /dev/sg1         Second generic SCSI device
  4812.                    : : :
  4813.  
  4814.  22    char    Digiboard serial card
  4815. 110                                      Appendix C. The Linux Device List
  4816.  
  4817.                  0 /dev/ttyD0       First Digiboard port
  4818.                  1 /dev/ttyD1       Second Digiboard port
  4819.                    : : :
  4820.  
  4821.         block   Second MFM, RLL and IDE hard disk/CD-ROM interface
  4822.  
  4823.                  0 /dev/hdc         Master: whole disk (or CD-ROM)
  4824.                  64/dev/hdd         Slave: whole disk (or CD-ROM)
  4825.  
  4826. Partitions are handled the same way as for the first interface (see major number
  4827. 3).
  4828.  
  4829.  23     char    Digiboard serial card - alternate devices
  4830.  
  4831.                  0 /dev/cud0        Callout device corresponding to ttyD0
  4832.                  1 /dev/cud1        Callout device corresponding to ttyD1
  4833.                    : : :
  4834.  
  4835.         block   Mitsumi proprietary CD-ROM
  4836.  
  4837.                  0 /dev/mcd         Mitsumi CD-ROM
  4838.  
  4839.   24    char    Stallion serial card
  4840.  
  4841.                  0 /dev/ttyE0       Stallion port 0 board 0
  4842.                  1 /dev/ttyE1       Stallion port 1 board 0
  4843.                    : : :
  4844.                  64/dev/ttyE64      Stallion port 0 board 1
  4845.                  65/dev/ttyE65      Stallion port 1 board 1
  4846.                    : : :
  4847.                 128/dev/ttyE128     Stallion port 0 board 2
  4848.                 129/dev/ttyE129     Stallion port 1 board 2
  4849.                    : : :
  4850.                 192/dev/ttyE192     Stallion port 0 board 3
  4851.                 193/dev/ttyE193     Stallion port 1 board 3
  4852.                    : : :
  4853.  
  4854.         block   Sony CDU-535 CD-ROM
  4855.  
  4856.                  0 /dev/cdu535      Sony CDU-535 CD-ROM
  4857.  
  4858.  25     char    Stallion serial card - alternate devices
  4859.  
  4860.                  0 /dev/cue0        Callout device corresponding to ttyE0
  4861.                  1 /dev/cue1        Callout device corresponding to ttyE1
  4862. C.3.  Minor numbers                                                     111
  4863.  
  4864.                    : : :
  4865.                 64 /dev/cue64       Callout device corresponding to ttyE64
  4866.                 65 /dev/cue65       Callout device corresponding to ttyE65
  4867.                    : : :
  4868.                128 /dev/cue128      Callout device corresponding to ttyE128
  4869.                129 /dev/cue129      Callout device corresponding to ttyE129
  4870.                    : : :
  4871.                192 /dev/cue192      Callout device corresponding to ttyE192
  4872.                193 /dev/cue193      Callout device corresponding to ttyE193
  4873.                    : : :
  4874.  
  4875.        block   First Matsushita (Panasonic/SoundBlaster) CD-ROM
  4876.  
  4877.                  0 /dev/sbpcd0      Panasonic CD-ROM controller 0 unit 0
  4878.                  1 /dev/sbpcd1      Panasonic CD-ROM controller 0 unit 1
  4879.                  2 /dev/sbpcd2      Panasonic CD-ROM controller 0 unit 2
  4880.                  3 /dev/sbpcd3      Panasonic CD-ROM controller 0 unit 3
  4881.  
  4882.  26    char    Frame grabbers
  4883.  
  4884.                  0 /dev/wvisfgrab   Quanta WinVision frame grabber
  4885.  
  4886.        block   Second Matsushita (Panasonic/SoundBlaster) CD-ROM
  4887.  
  4888.                  0 /dev/sbpcd4      Panasonic CD-ROM controller 1 unit 0
  4889.                  1 /dev/sbpcd5      Panasonic CD-ROM controller 1 unit 1
  4890.                  2 /dev/sbpcd6      Panasonic CD-ROM controller 1 unit 2
  4891.                  3 /dev/sbpcd7      Panasonic CD-ROM controller 1 unit 3
  4892.  
  4893.  27    char    QIC-117 tape
  4894.  
  4895.                  0 /dev/rft0        Unit 0, rewind-on-close
  4896.                  1 /dev/rft1        Unit 1, rewind-on-close
  4897.                  2 /dev/rft2        Unit 2, rewind-on-close
  4898.                  3 /dev/rft3        Unit 3, rewind-on-close
  4899.                  4 /dev/nrft0       Unit 0, no rewind-on-close
  4900.                  5 /dev/nrft1       Unit 1, no rewind-on-close
  4901.                  6 /dev/nrft2       Unit 2, no rewind-on-close
  4902.                  7 /dev/nrft3       Unit 3, no rewind-on-close
  4903. 112                                      Appendix C. The Linux Device List
  4904.  
  4905.  
  4906.  
  4907.         block   Third Matsushita (Panasonic/SoundBlaster) CD-ROM
  4908.  
  4909.                  0 /dev/sbpcd8      Panasonic CD-ROM controller 2 unit 0
  4910.                  1 /dev/sbpcd9      Panasonic CD-ROM controller 2 unit 1
  4911.                  2 /dev/sbpcd10     Panasonic CD-ROM controller 2 unit 2
  4912.                  3 /dev/sbpcd11     Panasonic CD-ROM controller 2 unit 3
  4913.  
  4914.  28     char    Stallion serial card - card programming
  4915.  
  4916.                  0 /dev/staliomem0  First Stallion I/O card memory
  4917.                  1 /dev/staliomem1  Second Stallion I/O card memory
  4918.                  2 /dev/staliomem2  Third Stallion I/O card memory
  4919.                  3 /dev/staliomem3  Fourth Stallion I/O card memory
  4920.  
  4921.         block   Fourth Matsushita (Panasonic/SoundBlaster) CD-ROM
  4922.  
  4923.                  0 /dev/sbpcd12     Panasonic CD-ROM controller 3 unit 0
  4924.                  1 /dev/sbpcd13     Panasonic CD-ROM controller 3 unit 1
  4925.                  2 /dev/sbpcd14     Panasonic CD-ROM controller 3 unit 2
  4926.                  3 /dev/sbpcd15     Panasonic CD-ROM controller 3 unit 3
  4927.  
  4928.         block   ACSI disk (68k)
  4929.  
  4930.                  0 /dev/ada         First ACSI disk whole disk
  4931.                  16/dev/adb         Second ACSI disk whole disk
  4932.                  32/dev/adc         Third ACSI disk whole disk
  4933.                    : : :
  4934.                 240/dev/adp         Sixteenth ACSI disk whole disk
  4935.  
  4936. Partitions are handled in the same way as for IDE disks (see major number 3)
  4937. except that the limit on logical partitions is 11 rather than 59 per disk.
  4938.  
  4939.  29     char    Universal frame buffer
  4940.  
  4941.                  0 /dev/fb0current  First frame buffer
  4942.                  1 /dev/fb0autodetect
  4943.                    : : :
  4944.                  16/dev/fb1current  Second frame buffer
  4945.                  17/dev/fb1autodetect
  4946.                    : : :
  4947. C.3.  Minor numbers                                                     113
  4948.  
  4949.  
  4950.  
  4951. The universal frame buffer device is currently supported only on Linux/68k.  The
  4952. current device accesses the frame buffer at current resolution; the autodetect
  4953. one at bootup (default) resolution. Minor numbers 2-15 within each frame buffer
  4954. assignment are used for specific device-dependent resolutions. There appears to
  4955. be no standardnaming for these devices.
  4956.  
  4957.        block   Aztech/Orchid/Okano/Wearnes CD-ROM
  4958.  
  4959.                  0 /dev/aztcd       Aztech CD-ROM
  4960.  
  4961.  30    char    iBCS-2 compatibility devices
  4962.  
  4963.                  0 /dev/socksys     Socket access
  4964.                  1 /dev/spx         SVR3 local X interface
  4965.                  2 /dev/inet/arp    Network access
  4966.                  2 /dev/inet/icmp   Network access
  4967.                  2 /dev/inet/ip     Network access
  4968.                  2 /dev/inet/udp    Network access
  4969.                  2 /dev/inet/tcp    Network access
  4970.  
  4971. iBCS-2 requires /dev/nfsd to be a link to /dev/socksys and /dev/X0R to be a link
  4972. to /dev/null.
  4973.  
  4974.        block   Philips LMS CM-205 CD-ROM
  4975.  
  4976.                  0 /dev/cm205cd     Philips LMS CM-205 CD-ROM
  4977.  
  4978. /dev/lmscd is an older name for this drive.  This driver does not work with the
  4979. CM-205MS CD-ROM.
  4980.  
  4981.  31    char    MPU-401 MIDI
  4982.  
  4983.                  0 /dev/mpu401data  MPU-401 data port
  4984.                  1 /dev/mpu401stat  MPU-401 status port
  4985.  
  4986.        block   ROM/flash memory card
  4987.  
  4988.                  0 /dev/rom0        First ROM card (rw)
  4989.                    : : :
  4990.                  7 /dev/rom7        Eighth ROM card (rw)
  4991.                  8 /dev/rrom0       First ROM card (ro)
  4992.  114                                      Appendix C. The Linux Device List
  4993.  
  4994.                     : : :
  4995.                   15/dev/rrom0       Eighth ROM card (ro)
  4996.                   16/dev/flash0      First flash memory card (rw)
  4997.                     : : :
  4998.                   23/dev/flash7      Eighth flash memory card (rw)
  4999.                   24/dev/rflash0     First flash memory card (ro)
  5000.                     : : :
  5001.                   31/dev/rflash7     Eighth flash memory card (ro)
  5002.  
  5003.  The read-write (rw) devices support back-caching written data in RAM, as well
  5004. as writing to flash RAM devices. The read-only devices (ro) support reading
  5005. only.
  5006.  
  5007.   32     block   Philips LMS CM-206 CD-ROM
  5008.  
  5009.                   0 /dev/cm206cd     Philips LMS CM-206 CD-ROM
  5010.  
  5011.   33     block   Modular RAM disk
  5012.  
  5013.                   0 /dev/ram0        First modular RAM disk
  5014.                   1 /dev/ram1        Second modular RAM disk
  5015.                     : : :
  5016.                  255/dev/ram255      256th modular RAM disk
  5017.  
  5018.   34-223        Unallocated
  5019.  
  5020. 224 -254        Local/experimental use
  5021.  
  5022.    For devices not assigned official numbers, this range should be used, in
  5023. order to avoid conflict with future assignments.  Please note that MAX_CHRDEV
  5024. and MAX_BLKDEV in linux/include/linux/major.h must be set to a value greater
  5025. than the highest used major number. For a kernel using local/experimental
  5026. devices, it is probably easiest to set both of these equal to 256. The memory
  5027. cost above using the default value of 64 is 3K.
  5028.  
  5029. 255              Reserved
  5030. C.4.  Additional /dev directory entries                                        
  5031. 115
  5032.  
  5033. C.4    Additional /dev directory entries
  5034.  
  5035. This section details additional entries that should or may exist in the /dev
  5036. directory. It is preferred that symbolic links use the same form (absolute or
  5037. relative) as is indicated here. Links are classified as hard or symbolic
  5038. depending on the preferredtype of link; if possible, the indicated type of link
  5039. should be used.
  5040. C.4.1  Compulsory links
  5041.  
  5042. These links should exist on all systems:
  5043.  
  5044. /dev/fd          /proc/self/fd    symbolic    File descriptors
  5045. /dev/stdin       fd/0             symbolic    Standard input file descriptor
  5046. /dev/stdout      fd/1             symbolic    Standard output file descriptor
  5047. /dev/stderr      fd/2             symbolic    Standard error file descriptor
  5048.  
  5049. C.4.2  Recommended links
  5050.  
  5051. It is recommended that these links exist on all systems:
  5052.  
  5053. /dev/X0R         null             symbolic    Used by iBCS-2
  5054. /dev/nfsd        socksys          symbolic    Used by iBCS-2
  5055. /dev/core        /proc/kcore      symbolic    Backward compatibility
  5056. /dev/scd?        sr?              hard       Alternate name for CD-ROMs
  5057.  
  5058. C.4.3  Locally defined links
  5059.  
  5060. The following links may be established locally to conform to the configuration
  5061. of the system. This is merely a tabulation of existing practice, and does not
  5062. constitute are commendation. However, if they exist, they should have the
  5063. following uses.
  5064.  
  5065. /dev/mouse       mouse port        symbolic    Current mouse device
  5066. /dev/tape        tape device       symbolic    Current tape device
  5067. /dev/cdrom       CD-ROM device     symbolic    Current CD-ROM device
  5068. /dev/modem       modem port        symbolic    Current dialout device
  5069. /dev/root        root device       symbolic    Current root filesystem
  5070. 116                                      Appendix C. The Linux Device List
  5071.  
  5072. /dev/swap        swap device       symbolic    Current swap device
  5073.  
  5074. /dev/modem should not be used for a modem which supports dialin as well as
  5075. dialout, as it tends to cause lock file problems. If it exists, /dev/modem
  5076. should point to theappropriate dialout (alternate) device.
  5077. C.4.4  Sockets and pipes
  5078.  
  5079. Non-transient sockets or named pipes may exist in /dev. Common entries are:
  5080.  
  5081. /dev/printer     socket            lpd local socket
  5082. /dev/log         socket            syslog local socket
  5083.  
  5084.  
  5085. Bibliography
  5086.  
  5087.  
  5088. [Car95]   R'emy  Card.   The  second  extended  filesystem:    current  state, 
  5089.           future development,  1995.   Slides used during presentation at the
  5090.           Second International  Linux and Internet Conference, in Berlin, May
  5091.           1995. Available  via  anonymous  FTP  from  ftp.ibp.fr,  in  the
  5092.           directory /pub/linux/packages/ext2fs/slides/berlin. 
  5093.  
  5094. [NSS89]   Evi Nemeth, Garth Snyder, and Scott Seebass. UNIX System
  5095.           Administration Handbook. Prentice-Hall, 1989. From Anonymous: I
  5096.           haven't seen any others to compare this one to, so I don't know that
  5097.           I'd particularly recommend it. It does cover both BSD and SYSV,
  5098.           though, so it might be more useful to a Linux sysadmin than a single
  5099.           book that focussed on BSD or SYSV exclusively. 
  5100.  
  5101. [POL93]   Jerry Peek, Tim O'Reilly, and Mike Loukide. UNIX Power Tools. Bantam,
  5102.           1993. From Anonymous: Not a comprehensive guide to much of anything,
  5103.           but it does include a LOT of hints and tips at the sysadmin level.
  5104.           This comes with a CD-ROM full of useful Unix programs, too. 
  5105.  
  5106. [Qui95]   Daniel Quinlan. Linux Filesystem Structure_Release 1.2, March 1995. A
  5107.           description of and a proposal for a standard Linux directory tree,
  5108.           with the intention is to make it easier to package software and
  5109.           administer Linux systems by making files appear in standard places.
  5110.           Follows fairly closely traditional Unix practice, and has got support
  5111.           from most Linux distributions.  Available via FTP from ftp.funet.fi,
  5112.           directory /pub/Linux/doc/fsstnd. 
  5113.  
  5114. [Ray91]   Eric Raymond, editor. The New Hacker's Dictionary. MIT Press, 1991. A
  5115.           dictionary of the slang and jargon used by hackers. A book version of
  5116.           the Jargon File, which contains all the text of the book (typically in
  5117.           a more up-to-date form), and which is in the public domain.
  5118.  
  5119.  
  5120.  
  5121.                                   117
  5122.